2012-02-25 80 views
2

我有一個問題,我不明白,似乎沒有一個簡單的方法來調試問題。我相信這很簡單。mvc3提交模型空

@model StartStop.ServiceResources.UserSettings 

我的MVC3視圖綁定了一個特定的模型;

public class Setting 
{ 
    public Int64 SettingID { get; set; } 
    public Int64 UserID { get; set; } 
    public int PreferenceType { get; set; } 
    public string PreferenceName { get; set; } 
    public bool PreferenceBool { get; set; } 
    public int PreferenceInt { get; set; } 
    public string PreferenceString { get; set; } 
    public DateTime CreatedOn { get; set; } 
    public DateTime ModifiedOn { get; set; } 

} 

public class UserSettings 
{ 
    public Int64 UserID { get; set; } 
    public List<Setting> Settings { get; set; } 
} 

該視圖列出了代表該列表的複選框;

@using (Html.BeginForm("ManageAccount","Account", FormMethod.Post)) 
     { 
      <table class="tbl" cellspacing="0"> 

       <tr> 
        <th>Preference</th> 
        <th>Setting</th> 
       </tr> 

       @if (Model != null) 
       { 
        foreach (var item in Model.Settings.ToList()) 
        { 
         <tr> 
          <td>@item.PreferenceName 
          </td> 
          <td> 
           @if (item.PreferenceType == 2) 
           { 
            @Html.CheckBoxFor(modelItem => item.PreferenceBool) 
           } 
          </td> 
         </tr> 

        } 
       } 
      </table> 

      <input type="submit" value="Save Changes" class="action medium" /> 

     } 

一切都很好,我將數據加載到視圖中,呈現視圖並選取正確的設置。但是,當我在底部發布帖子時,視圖模型返回null!我不知道爲什麼...

[HttpPost] 
    [Authorize] 
    public ActionResult ManageAccount(StartStop.ServiceResources.UserSettings model) 
    { 
     if (ModelState.IsValid) 
     { 

      foreach (StartStop.ServiceResources.Setting oSetting in model.Settings) 
      { 
       StartStop.Helpers.UserPreferences.SaveUserSetting(oSetting); 
      } 
     } 
     return View(model); 
    } 

任何人都可以幫忙嗎?

回答

5

的問題是在您查看以下行:

@Html.CheckBoxFor(modelItem => item.PreferenceBool) 

我看到人們寫了以下lambda表達式modelItem => item.SomeProperty在他們的觀點經常和問爲什麼模型綁定不上不正確綁定集合屬性他們的觀點模型。

這不會爲複選框生成正確的名稱,以便默認的模型聯編程序能夠重新創建設置集合。我建議您閱讀following blog post以更好地理解模型聯編程序所期望的正確格式。

嘗試這樣的:

@model StartStop.ServiceResources.UserSettings 
@using (Html.BeginForm("ManageAccount", "Account", FormMethod.Post)) 
{ 
    <table class="tbl" cellspacing="0"> 
     <tr> 
      <th>Preference</th> 
      <th>Setting</th> 
     </tr> 

     @if (Model != null) 
     { 
      for (var i = 0; i < Model.Settings.Count; i++) 
      { 
       <tr> 
        <td>@Model.Settings[i].PreferenceName</td> 
        <td> 
         @if (Model.Settings[i].PreferenceType == 2) 
         { 
          @Html.CheckBoxFor(x => x.Settings[i].PreferenceBool) 
         } 
        </td> 
       </tr> 
      } 
     } 
    </table> 

    <input type="submit" value="Save Changes" class="action medium" /> 
} 

這是說,我會建議你使用編輯模板,像這樣:

@using (Html.BeginForm("ManageAccount","Account", FormMethod.Post)) 
{ 
    <table class="tbl" cellspacing="0"> 
     <tr> 
      <th>Preference</th> 
      <th>Setting</th> 
     </tr> 

     @if (Model != null) 
     { 
      @Html.EditorFor(x => x.Settings) 
     } 
    </table> 

    <input type="submit" value="Save Changes" class="action medium" /> 
} 

,然後定義將automatcially呈現自定義編輯模板Settings集合中的每個元素(~/Views/Shared/EditorTemplates/Setting.cshtml):

@model StartStop.ServiceResources.Setting 
<tr> 
    <td>@Model.PreferenceName</td> 
    <td> 
     @if (Model.PreferenceType == 2) 
     { 
      @Html.CheckBoxFor(x => x.PreferenceBool) 
     } 
    </td> 
</tr> 

此外,我可以在此窗體中看到的唯一輸入字段是與您的模型上的PreferenceBool屬性綁定的複選框。因此,在您的POST控制器操作中,您將獲得初始化的Settings列表屬性,但不要指望爲此類Setting類中的其他屬性找到任何值,除非您在表單中包含輸入字段(更確切地說,在編輯器中我已經展示的模板)。

+0

感謝您花時間詳細回覆,我真的很感激!我將嘗試這兩種方法並將反饋給您。 – philbird 2012-02-25 11:26:50

+0

我嘗試了第一種方法和成功。感謝關於第二種方法的信息,但這對我來說並不成功,我不確定是否需要我遍歷xhtml for中的設置。無論如何,現在所有排序和教訓都很好學。謝謝。 – philbird 2012-02-25 15:32:41