HTML.TextBoxFor已禁用,未传递值

Bar*_*ens 7 c# asp.net-mvc-4

我认为我有以下几点:

@Html.TextBoxFor(model => model.PersonnelId, new { disabled = "disabled" })
Run Code Online (Sandbox Code Playgroud)

在我的控制器中我有这个:

if (ModelState.IsValid)
{
    PersonnelFacade.SavePerson(person);
    return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)

现在,当我检查person.PersonnelId它是空的.当我删除{disabled ="disabled"}它工作正常但我可以更改PersonnelId,这不是我想要做的.

我究竟做错了什么?

小智 10

尝试2个值

  1. PersonnelIdForDisplay
  2. PersonnelId

然后在你的观点

  1. @ Html.TextBoxFor(model => model.PersonnelIdForDisplay,new {disabled ="disabled"})
  2. @ Html.HiddenFor(p => p.PersonnelId)


Jus*_*vey 5

请尝试将其设为只读.

@Html.TextBoxFor(model => model.PersonnelId, new { readonly= "readonly" })
Run Code Online (Sandbox Code Playgroud)

  • “只读”工作正常,除了文本框仍处于启用状态。当我添加“禁用”时,它再次失去了价值。 (2认同)