2011-04-01 49 views
0

我试图构建一个视图来编辑MVC中的一个模型,但是当我在我的“编辑”方法中获取编辑后的模型时,某些属性为null,有些具有不正确的值。MVC3模型编辑 - 返回到控制器的不正确/缺失值

我已经把它分解成一个非常基本的例子,但我仍然无法弄清楚发生了什么。当我编辑我的模型并单击保存时,控制器中返回的模型具有错误的布尔值(它始终为false)。此外,我的父母和IList对象始终为空。真的,我永远不想编辑这些字段,在这个模型上我只想编辑Name和Active属性。然而,它被返回为null,并且我的连贯的NHibernate SaveOrUpdate()方法试图删除它们。

所以我的问题是,为什么我的布尔字段总是返回false。为什么我的Parent和IList字段总是返回null?

这是我的控制器:

public ActionResult Edit(int id) { 
    var clientDetail = clientRepository.Get(id); 
    return View(clientDetail); 
} 
[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit(int id, Client editedItem) { 
    try { 
     ValidateModel(editedItem); 
     if (this.ModelState.IsValid) { 
      clientRepository.SaveOrUpdate(editedItem); 
      return RedirectToAction("Details", new { id = id }); 
     } 
    } catch (Exception ex) { 
     //Need to show some type of warning here...perhaps in a view bag. 
    } 
    return RedirectToAction("Index"); 
} 

而我的观点:

@model Models.Client 

<h2>Edit</h2> 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    @Html.EditorForModel() 
    <input type="submit" value="Save" /> 
} 

型号:

public class Client { 
    [Display(Name="Client ID")] 
    [DataType("Url")] 
    [DisplayFormat(DataFormatString = "../../Client/Details/{0}/")] 
    public virtual int Id { get; set; } 

    public virtual Client Parent { get; set; } 

    public virtual string Name { get; set; } 

    [Display(Name = "Active")] 
    public virtual bool Active { get; set; } 

    [ScaffoldColumn(false)] 
    [Editable(false)] 
    public virtual IList<Login> Logins { get; set; } 

} 

UPDATE:生成的HTML

<form action="/Client/Edit/17" method="post"> 
    <table class="editor-table"> 
    <tr> 
     <td style="width: 10em"> 
     <div class="editor-label" style="text-align: right;"> 
      * <label for="Id">Client ID:</label> 
     </div> 
     </td> 
     <td> 
     <div class="editor-field"> 
      <input class="" data-val="true" data-val-number="The field Client ID must be a number." data-val-required="The Client ID field is required." id="Id" name="Id" type="text" value="17"> <span class="field-validation-valid" data-valmsg-for="Id" data-valmsg-replace="false">*</span> 
     </div> 
     </td> 
    </tr> 
    </table> 
    <table class="editor-table"> 
    <tr> 
     <td style="width: 10em"> 
     <div class="editor-label" style="text-align: right;"> 
      <label for="Name">Name:</label> 
     </div> 
     </td> 
     <td> 
     <div class="editor-field"> 
      <input class="" id="Name" name="Name" type="text" value="Dummy Client"> <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="false">*</span> 
     </div> 
     </td> 
    </tr> 
    </table> 
    <table class="editor-table"> 
    <tr> 
     <td style="width: 10em"> 
     <div class="editor-label" style="text-align: right;"> 
      * <label for="Active">Active:</label> 
     </div> 
     </td> 
     <td> 
     <div class="editor-field"> 
      <input class="check-box" type="checkbox" checked="'checked'"> <span class="field-validation-valid" data-valmsg-for="Active" data-valmsg-replace="false">*</span> 
     </div> 
     </td> 
    </tr> 
    </table><input type="submit" value="Save"> 
</form> 

编辑模板:

模板:

@if (!ViewData.ModelMetadata.HideSurroundingHtml) { 
    <table class="editor-table"> 
    <tr> 
     <td style="width: 10em"> 
      <div class="editor-label" style="text-align: right;"> 
       @(ViewData.ModelMetadata.IsRequired ? "*" : "") 
       <label for="@ViewData.ModelMetadata.PropertyName">@ViewData.ModelMetadata.GetDisplayName():</label> 
      </div> 
     </td> 
     <td> 
      <div class="editor-field"> 
       @RenderSection("EditContent") 
       @Html.ValidationMessage("", "*") 
      </div> 
     </td> 
    </tr> 
</table> 
} 

布尔模板:

@{ 
    Layout = "~/Views/Shared/EditorTemplates/_Template.cshtml"; 
} 

@section EditContent { 
    @if (ViewData.ModelMetadata.IsNullableValueType) { 
     <select class="list-box tri-state" > 
      <option value="@Model.HasValue ? " : "selected='selected'">Not Set</option> 
      <option value="@Model.HasValue && @Model.Value ? "selected='selected'">True</option> 
      <option value="@Model.HasValue && [email protected] ? "selected='selected'">False</option> 
     </select> 
    } else { 
     <input class="check-box" id="@ViewData.ModelMetadata.PropertyName" name="@ViewData.ModelMetadata.PropertyName" type="checkbox" @(Model ? "checked='checked'" : "") /> 
    } 
} 
+0

你的post方法不需要'id'参数,只需要Client对象就足够了,默认的模型绑定器可能会与这个额外的参数有点混淆。 – 2011-04-01 20:50:07

+0

@Matt - 感谢您的提示。但是这对这个问题没有任何影响。编辑后的模型将其放入控制器中,但布尔型字段始终返回false,并且我的复杂类型将返回为null。 – Brosto 2011-04-01 20:53:53

+0

你可以显示由视图生成的HTML吗?它看起来像'EditorForModel'没有按预期生成表单字段。 – 2011-04-01 20:58:08

回答

0

我能够通过改变EditorTemplate一个简单的用我的布尔字段来解决问题:@Html。复选框( “”,模型)。虽然这对于可空布尔值不起作用,但就我而言,我没有任何担心。这是我的新的编辑器模板的样子:

@{ 
    Layout = "~/Views/Shared/EditorTemplates/_Template.cshtml"; 
} 

@model Boolean 

@section EditContent { 
    @Html.CheckBox("", Model) 
} 

而生成的HTML:

<table class="editor-table"> 
    <tr> 
     <td style="width: 10em"> 
      <div class="editor-label" style="text-align: right;"> 
       * 
       <label for="Active">Active:</label> 
      </div> 
     </td> 
     <td> 
      <div class="editor-field"> 
    <input checked="checked" data-val="true" data-val-required="The Active field is required." id="Active" name="Active" type="checkbox" value="true" /><input name="Active" type="hidden" value="false" /> 
       <span class="field-validation-valid" data-valmsg-for="Active" data-valmsg-replace="false">*</span> 
      </div> 
     </td> 
    </tr> 
</table> 

希望这可以帮助其他人避免我经历了,但它肯定是一个学习的经验。大家,感谢上面所有有用的建议。