简体   繁体   English

"ASP.NET MVC Post 模型项为空"

[英]ASP.NET MVC Post model item is null

I have an edit view that I want to only allow certain fields to be edited and updated.我有一个编辑视图,我只想允许编辑和更新某些字段。 My issue, for appearance reasons I want the non editable field to be text only without a textbox around them.我的问题,出于外观原因,我希望不可编辑的字段仅是文本,周围没有文本框。

I am displaying the data in the view as @Model.Customer<\/code> .我将视图中的数据显示为@Model.Customer<\/code> 。 This value I do not want to update and want it displayed just as text.这个值我不想更新,希望它显示为文本。

@Html.TextBoxFor(m => m.Address)<\/code> is an editable field that I want to use to update the data. @Html.TextBoxFor(m => m.Address)<\/code>是我想用来更新数据的可编辑字段。

When I submit the view back to the controller the Model.Customer<\/code> is now null.当我将视图提交回控制器时, Model.Customer<\/code>现在为空。 Model.Address<\/code> is correct. Model.Address<\/code>是正确的。 Is there anyway I can use @Model.Customer<\/code> and keep the Model.Customer<\/code> data when posting to the controller or am I stuck using the @Html.TextBoxFor(m => m.Customer)<\/code> to make this work correctly?无论如何我可以使用@Model.Customer<\/code>并在发布到控制器时保留Model.Customer<\/code>数据,还是我坚持使用@Html.TextBoxFor(m => m.Customer)<\/code>以使其正常工作?

Controller:控制器:

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 });
    }
}

try to add html.hiddenfor尝试添加 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM