[英]ASP.NET MVC Post model item is null
我有一個編輯視圖,我只想允許編輯和更新某些字段。 我的問題,出於外觀原因,我希望不可編輯的字段僅是文本,周圍沒有文本框。
我將視圖中的數據顯示為
@Model.Customer<\/code> 。
這個值我不想更新,希望它顯示為文本。
@Html.TextBoxFor(m => m.Address)<\/code>是我想用來更新數據的可編輯字段。
當我將視圖提交回控制器時,
Model.Customer<\/code>現在為空。
Model.Address<\/code>是正確的。
無論如何我可以使用
@Model.Customer<\/code>並在發布到控制器時保留
Model.Customer<\/code>數據,還是我堅持使用
@Html.TextBoxFor(m => m.Customer)<\/code>以使其正常工作?
控制器:
public ActionResult CreateExisting(int id)
{
var LO = db.LOes.Find(id);
return View(LO);
}
[HttpPost]
public ActionResult CreateExisting(int id, LO lo)
{
try
{
db.Entry(lo).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index", "LO");
}
catch
{
return RedirectToAction("Error", new { id = id });
}
}
嘗試添加 html.hiddenfor
@using (Html.BeginForm("CreateExisting", "..your controller", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model=> model.ID);
<table style="width:800px">
<tr>
<td>
@Html.DisplayFor(model => model.Customer)
</td>
<td>
@Html.TextBoxFor(model => model.Address)
</td>
</tr>
</table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.