我正在用 MVC3 编写一个调查应用程序。 我有这个类(class)(简化):
public class Survey
{
public Respondent Respondent { get; set; }
}
在我看来:
@model Survey
// bla bla bla
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.Partial("_Respondent", Model.Respondent)
}
当我发回时,survey.Respondent = null :(
[HttpPost]
public ActionResult Survey(Survey survey)
{
return RedirectToAction("Index");
}
_Respondednt 部分 View 的简化代码:
@model Mercer.FITT.Respondent
<fieldset>
<legend>Respondent Information</legend>
<table>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.JobTitle)
</td>
<td>
@Html.EditorFor(model => model.JobTitle)
@Html.ValidationMessageFor(model => model.JobTitle)
</td>
</tr>
</table>
</fieldset>
如果我摆脱部分 View 并将部分 View 内容复制到主视图中,则一切都很好。知道为什么不从部分 View 中收集数据吗? 也许我应该以不同的方式调用部分 View ?
非常感谢。
请您参考如下方法:
使用EditorTemplate代替部分 View
@model Survey
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.EditorFor(m=>m.Respondent)
}
并且不要忘记在 EditorTemplates
文件夹中创建 Respondent.cshtml
文件,并将要编辑的受访者字段放入其中。