gpt4 book ai didi

c# - @Html.HiddenFor 在 HttpPost 中导致 null

转载 作者:太空宇宙 更新时间:2023-11-03 20:59:52 25 4
gpt4 key购买 nike

请看下面的代码:

 @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 在 Controller 中始终为 null。 Role.Description 包含正确的值。

角色不会更新,因为 Role.Id 为空。有什么问题?

最佳答案

从基类来看,Id 属性没有public setter 并且不能由模型绑定(bind)器设置。参见 here

关于c# - @Html.HiddenFor 在 HttpPost 中导致 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46608252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com