我见过这个问题的多个实例。尽管如此,我仍然无法弄清楚这个post方法没有将模型传回控制器的问题。我已经查看了传递给控制器的模型之间的命名约定,以及如果我删除它们是否传入了只读字段。除此之外,它可能是BindAttribute,将多选的RoleIds绑定到模型。
这是我的代码:
模特:
public class UserDetail
{
public Guid Id { get; set; }
public List<string> RoleIds { get; set; }
public string DomainName { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string EmailAddress { get; set; }
public string PhysicalDeliveryOfficeName { get; set; }
public string TenantDisplayId { get; set; }
public Boolean InitPassword { get; set; }
public Boolean Active { get; set; }
public Boolean SystemAdmin { get; set; }
public ICollection<RoleDetail> Roles { get; set; }
public IEnumerable<SelectedRoles> SelectedRoles { get; set; }
public MultiSelectList RoleOptions { get; set; }
public UserDetail()
{
Id = Id;
RoleIds = RoleIds;
FirstName = FirstName;
LastName = LastName;
Name = Name;
Active = Active;
EmailAddress = EmailAddress;
SystemAdmin = SystemAdmin;
}
}
cshtml:
@model IzendaEmbedded.Models.UserDetail
@using (Html.BeginForm("EditActiveDirectoryUser","ActiveDirectory",
FormMethod.Post, new {userDetail = Model}))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<nav aria-label="breadcrumb">
<ol class="breadcrumb" style="height: 100px; padding-top: 20px; padding-left: 140px; background-color: #FFFFFF">
<li class="breadcrumb-item" style="font-family: arial; font-size:40px"><a href=@Url.Action("Index", "Home")>Izenda</a></li>
<li class="breadcrumb-item active" aria-current="page" style="font-family: arial; font-size:40px"><a href=@Url.Action("Parse", "ActiveDirectory")>Active Directory</a></li>
<li class="breadcrumb-item active" aria-current="page" style="font-family: arial; font-size:40px">Edit Izenda User</li>
</ol>
</nav>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserName, "Account Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, "First Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, "Last Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailAddress, "Email Address", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.EmailAddress, "", 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-4">
@Html.ListBoxFor(model => model.RoleIds, Model.RoleOptions, new {@class = "form-control"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
<div class="checkbox">
@Html.EditorFor(model => model.Active)
@Html.ValidationMessageFor(model => model.Active, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-4">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
控制器发布方法:
[HttpPost]
public async Task<ActionResult> EditActiveDirectoryUser([Bind(Include =
"Id, Name, RoleIds")] UserDetail userDetail)
{
//save logic
}