2012-02-25 8 views
2

私には分かりませんが、問題を簡単にデバッグする方法はないようです。私はそれが簡単だと確信しています。mvc3 submit model empty

@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) 

私は、人々は非常に頻繁に自分の意見で、次のラムダ式modelItem => item.SomePropertyを書き込み、モデルバインダーが正しく上のコレクションのプロパティを結合しない理由を尋ねる見ます彼らのビューモデル。

デフォルトのモデルバインダーがSettingsコレクションを再作成できるように、これはチェックボックスに適切な名前を生成しません。モデルバインダーが期待している正しい形式をよりよく理解するために、following blog postを読むことをお勧めします。このような

試してみてください。

@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のためにレンダリングされるカスタムエディタのテンプレートを定義します。これは言われている

@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" /> 
} 

、私はそうのように、エディタのテンプレートを使用して、あなたをお勧めします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コントローラのアクションの内部では、設定リストプロパティは初期化されますが、このSettingクラスの他のプロパティの値は、フォームに入力フィールドが含まれていない限り私が示したテンプレート)。

+0

詳細についてお返事いただきありがとうございます。本当にありがとうございます。私は両方のアプローチを試みるつもりです、そして、あなたにフィードバックします。 – philbird

+0

私は最初のアプローチとSUCCESSを試みました。 2番目の方法についての情報をありがとう、しかし、これは私にとっては成功しませんでした、私はxhtmlの設定を反復する必要があるかどうかはわかりません。とにかく、今すぐソートされ、レッスンはうまく学習されました。ありがとうございました。 – philbird