@Html.HiddenFor 在 HttpPost 中导致 null
@Html.HiddenFor causes null in HttpPost
请看下面的代码:
@model NHibernate.AspNet.Identity.IdentityRole
@{
ViewBag.Title = "Edit";
Layout = "~/Areas/Administration/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div>
Role name
</div>
<p>
@Html.TextBoxFor(model => model.Name)
</p>
<input type="submit" value="Save" />
}
和下面的代码:
[HttpPost]
public ActionResult Edit(IdentityRole role)
{
Service.Update(role);
TempData["Comment"] = "The record was updated";
return RedirectToAction("Index");
}
Role.Id 在控制器中始终为空。 Role.Description 包含正确的值。
角色将不会更新,因为 Role.Id 为空。有什么问题?
根据基数 class,Id
属性 没有 public
setter 并且不能由模型绑定器设置。参见 here
请看下面的代码:
@model NHibernate.AspNet.Identity.IdentityRole
@{
ViewBag.Title = "Edit";
Layout = "~/Areas/Administration/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div>
Role name
</div>
<p>
@Html.TextBoxFor(model => model.Name)
</p>
<input type="submit" value="Save" />
}
和下面的代码:
[HttpPost]
public ActionResult Edit(IdentityRole role)
{
Service.Update(role);
TempData["Comment"] = "The record was updated";
return RedirectToAction("Index");
}
Role.Id 在控制器中始终为空。 Role.Description 包含正确的值。
角色将不会更新,因为 Role.Id 为空。有什么问题?
根据基数 class,Id
属性 没有 public
setter 并且不能由模型绑定器设置。参见 here