我有一個問題,我不明白,似乎沒有一個簡單的方法來調試問題。我相信這很簡單。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);
}
任何人都可以幫忙嗎?
感謝您花時間詳細回覆,我真的很感激!我將嘗試這兩種方法並將反饋給您。 – philbird 2012-02-25 11:26:50
我嘗試了第一種方法和成功。感謝關於第二種方法的信息,但這對我來說並不成功,我不確定是否需要我遍歷xhtml for中的設置。無論如何,現在所有排序和教訓都很好學。謝謝。 – philbird 2012-02-25 15:32:41