2011-04-01 51 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> 

希望這可以幫助其他人避免我經歷了,但它肯定是一個學習的經驗。大家,感謝上面所有有用的建議。