ASP.NET MVC 5 Multi select 在 POST 期间未绑定
ASP.NET MVC 5 Multi select not binding during POST
我是 ASP.NET MVC 5 的新手,在提交表单时 POST 期间仅绑定正文的 multi-select 部分时遇到了一些问题。表单正确呈现,复选框被正确 selected。表单使用 visual studio 构建(除了 multi-select)
public class EditViewModel
{
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "LastName")]
public string LastName { get; set; }
public IList<RolesViewModel> Roles { get; set; }
}
public class RolesViewModel
{
public string RoleId { get; set; }
public string RoleName { get; set; }
public bool Selected { get; set; }
}
[HttpGet]
public async Task<ActionResult> Edit(string Id)
{...
var model = Mapper.Map<ApplicationUser, EditViewModel>(user);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(EditViewModel model)
{
}
@model BloggingService.Web.Models.EditViewModel
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Edit</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@for (int i = 0; i < Model.Roles.Count; i++)
{
@Html.CheckBoxFor(x => @Model.Roles[i].Selected,"test1");
<div class="form-check">
<input type="hidden" asp-for="@Model.Roles[i].RoleId" />
<input type="hidden" asp-for="@Model.Roles[i].RoleName" />
<input type="checkbox" asp-for="@Model.Roles[i].Selected" class="form-check-input" checked="@Model.Roles[i].Selected" />
<label class="form-check-label" asp-for="@Model.Roles[i].Selected">
@Model.Roles[i].RoleName
</label>
</div>
}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
非常感谢任何见解。
我已经设法通过使用我的 for 循环中的 HTML 辅助方法找到了完成此类任务的正确且更简洁的方法,现在数据能够在数据绑定期间正确绑定。
@Html.HiddenFor(x => @Model.Roles[i].RoleId);
@Html.HiddenFor(x => @Model.Roles[i].RoleName);
@Html.CheckBoxFor(x => @Model.Roles[i].Selected);
我是 ASP.NET MVC 5 的新手,在提交表单时 POST 期间仅绑定正文的 multi-select 部分时遇到了一些问题。表单正确呈现,复选框被正确 selected。表单使用 visual studio 构建(除了 multi-select)
public class EditViewModel
{
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "LastName")]
public string LastName { get; set; }
public IList<RolesViewModel> Roles { get; set; }
}
public class RolesViewModel
{
public string RoleId { get; set; }
public string RoleName { get; set; }
public bool Selected { get; set; }
}
[HttpGet]
public async Task<ActionResult> Edit(string Id)
{...
var model = Mapper.Map<ApplicationUser, EditViewModel>(user);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(EditViewModel model)
{
}
@model BloggingService.Web.Models.EditViewModel
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Edit</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@for (int i = 0; i < Model.Roles.Count; i++)
{
@Html.CheckBoxFor(x => @Model.Roles[i].Selected,"test1");
<div class="form-check">
<input type="hidden" asp-for="@Model.Roles[i].RoleId" />
<input type="hidden" asp-for="@Model.Roles[i].RoleName" />
<input type="checkbox" asp-for="@Model.Roles[i].Selected" class="form-check-input" checked="@Model.Roles[i].Selected" />
<label class="form-check-label" asp-for="@Model.Roles[i].Selected">
@Model.Roles[i].RoleName
</label>
</div>
}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
非常感谢任何见解。
我已经设法通过使用我的 for 循环中的 HTML 辅助方法找到了完成此类任务的正确且更简洁的方法,现在数据能够在数据绑定期间正确绑定。
@Html.HiddenFor(x => @Model.Roles[i].RoleId);
@Html.HiddenFor(x => @Model.Roles[i].RoleName);
@Html.CheckBoxFor(x => @Model.Roles[i].Selected);