我有一个由以下组件组成的 MVC3 页面:
ProfileController - 包含所有操作并加载下面的所有视图
/Profile/Index - 此操作方法创建一个 UserViewModel 实例,其中包含多个属性和嵌套集合,包括一个名为“PrefPrograms”的“PreferencePrograms”类型,其定义为:
public class PreferencePrograms : List<PreferenceProgram>
“Index”操作方法填充 PrefPrograms 集合和 UserViewModel 的其余部分,并将该模型传递给强类型的“Index.cshtml”视图。
Index.cshtml - 这是“UserViewModel”类型的强类型视图。它由多个局部视图组成。此页面中名为“Preferences.cshtml”的部分视图提供了 PrefPrograms 集合的格式化显示。Index.cshtml 的相关部分如下。
@model ProfilePreferenceCenterProto.Models.UserViewModel
@{ Html.RenderPartial("Preferences", Model); }
Preferences.cshtml - 这个强类型的局部视图由 Index.cshtml 使用 Html.RenderPartial(上图)加载。在 Preferences.cshtml 中,我有一个 Begin.AjaxForm() 方法,用于发布到“PreferenceSubmit”操作,输入提交按钮位于该局部视图的底部。在局部视图中,我调用一个 @Html.EditorFor() 助手来为“PrefPrograms”集合中的每个“PreferenceProgramModel”项加载一个编辑器模板。
这是我的问题 - 所有项目都正确加载(包括组件的部分视图和编辑器),但是当我将 Preferences.cshtml 上的表单发布到“PreferencesSubmit”操作时,模型的值没有被传递(模型被实例化,但是值仅初始化 - 属性和集合值实际上并未传回控制器)。
Preferences.cshtml 部分视图如下所示。
@model ProfilePreferenceCenterProto.Models.UserViewModel
<div id="accordian">
@using(Ajax.BeginForm("PreferencesSubmit", "Profile", new AjaxOptions{ UpdateTargetId = "accordian" })){
<div id="accordion">
<ul class="tabs">
</ul>
<div class="panes">
<div>
@{
List<string> AffiliateNames = new List<string>();
foreach(ProfilePreferenceCenterProto.Models.PreferenceProgramModel list in Model.PrefPrograms)
{
AffiliateNames.Add(list.SubcategoryName);
}
IEnumerable<string> listNames = AffiliateNames.Distinct();
int counter = 0;
}
@foreach (string AccordionTabName in listNames)
{
<h2>@AccordionTabName</h2>
<div class="pane" @if (counter == 0){ <text>style="display:block;"</text> } >
<table>
<tr class="row">
<th class="name">Subscription</th>
<th class="icon">Email</th>
<th class="icon">SMS</th>
<th class="icon">Facebook</th>
<th class="icon">Mail</th>
<th class="icon">Phone</th>
</tr>
@{
counter++;
var TabPrograms = (from l in Model.PrefPrograms
where l.SubcategoryName == @AccordionTabName
select l);
}
@Html.EditorFor(m => TabPrograms)
</table>
</div>
}
</div>
</div>
</div>
<div align="center"><input type="submit" value="Save Preferences" /></div>
}
/Shared/EditorTemplates/PreferenceProgramModel.cshtml - “PreferenceProgramModel”项目的编辑器模板,定义如下:
@model ProfilePreferenceCenterProto.Models.PreferenceProgramModel
<tr class="row">
<td class="name">@Model.ListName</td>
<td class="icon">
@if (Model.EmailEnabled)
{
<a id="@Model.EmailFilterID" href="#" onclick="ImageClick(@Html.IdFor(m => m.EmailStatus));"><img height="25" width="28" src="@Model.Email_Icon_NotSelected" /></a>
}
</td>
<td class="icon">
@if (Model.SMSEnabled)
{
<a id="@Model.SMSFilterID" href="#"><img height="25" width="28" src="@Model.SMS_Icon_NotSelected" /></a>
}
</td>
<td class="icon">
@if (Model.FBEnabled)
{
<a id="@Model.FBFilterID" href="#"><img height="25" width="28" src="@Model.FB_Icon_NotSelected" /></a>
}
</td>
<td class="icon">
@if (Model.MailEnabled)
{
<a id="@Model.MailFilterID" href="#"><img height="25" width="28" src="@Model.Mail_Icon_NotSelected" /></a>
}
</td>
<td class="icon">
@if (Model.PhoneEnabled)
{
<a id="@Model.PhoneFilterID" href="#"><img height="25" width="28" src="@Model.Phone_Icon_NotSelected" /></a>
}
</td>
</tr>
@Html.HiddenFor(m => m.EmailStatus)
@Html.HiddenFor(m => m.SMSStatus)
@Html.HiddenFor(m => m.FBStatus)
@Html.HiddenFor(m => m.MailStatus)
@Html.HiddenFor(m => m.PhoneStatus)
<script type="text/javascript">
$(document).ready(function () {
function ImageClick(Resource) {
alert(Resource.attr("value"));
if (Resource.attr("value") != 1) {
Resource.val("1");
}
else {
Resource.val("2");
}
alert(Resource.attr("value"));
}
if ("@Model.EmailEnabled" == "True") {
$("#@Model.EmailFilterID").click(function () {
ImageClick($("#@Html.IdFor(m => m.EmailStatus)"));
return false;
});
}
if ("@Model.SMSEnabled" == "True") {
$("#@Model.SMSFilterID").click(function () {
ImageClick($("#@Html.IdFor(m => m.SMSStatus)"));
return false;
});
}
if ("@Model.FBEnabled" == "True") {
$("#@Model.FBFilterID").click(function () {
ImageClick($("#@Html.IdFor(m => m.FBStatus)"));
return false;
});
}
if ("@Model.MailEnabled" == "True") {
$("#@Model.MailFilterID").click(function () {
ImageClick($("#@Html.IdFor(m => m.MailStatus)"));
return false;
});
}
if ("@Model.PhoneEnabled" == "True") {
$("#@Model.PhoneFilterID").click(function () {
ImageClick($("#@Html.IdFor(m => m.PhoneStatus)"));
return false;
});
}
});
</script>
PreferencesSubmit 控制器动作是用这个签名定义的:
public ActionResult PreferencesSubmit(Models.UserViewModel model)
页面中呈现的表单标签如下:
<form action="/Profile/PreferencesSubmit" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#accordian" id="form0" method="post">
一些渲染的隐藏字段如下所示:
<input data-val="true" data-val-number="The field EmailStatus must be a number." data-val-required="The EmailStatus field is required." id="TabPrograms_0__EmailStatus" name="TabPrograms[0].EmailStatus" type="hidden" value="0" />
<input data-val="true" data-val-number="The field SMSStatus must be a number." data-val-required="The SMSStatus field is required." id="TabPrograms_0__SMSStatus" name="TabPrograms[0].SMSStatus" type="hidden" value="0" />
<input data-val="true" data-val-number="The field FBStatus must be a number." data-val-required="The FBStatus field is required." id="TabPrograms_0__FBStatus" name="TabPrograms[0].FBStatus" type="hidden" value="0" />
<input data-val="true" data-val-number="The field MailStatus must be a number." data-val-required="The MailStatus field is required." id="TabPrograms_0__MailStatus" name="TabPrograms[0].MailStatus" type="hidden" value="0" />
<input data-val="true" data-val-number="The field PhoneStatus must be a number." data-val-required="The PhoneStatus field is required." id="TabPrograms_0__PhoneStatus" name="TabPrograms[0].PhoneStatus" type="hidden" value="0" />
我已验证 ModelState.IsValid 在调用 PreferencesSubmit 时为真,但模型本身不包含页面中的实际值。如何在提交时将我的强类型模型值从部分视图传递回 Action 方法?