不从局部视图收集MVC3数据

mon*_*tro 2 c# partial-views asp.net-mvc-3

我正在MVC3中编写一个调查应用程序.我有这个课(简化):

public class Survey
{
    public Respondent Respondent { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在我看来:

@model Survey

// bla bla bla

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    @Html.Partial("_Respondent", Model.Respondent) 
}
Run Code Online (Sandbox Code Playgroud)

当我发回它时,调查.Respondent = null :(

[HttpPost]
public ActionResult Survey(Survey survey)
{
    return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)

_Respondednt局部视图的简化代码:

@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>
Run Code Online (Sandbox Code Playgroud)

如果我摆脱局部视图并将部分视图内容复制到主视图中,一切都很好.知道为什么不从局部视图中收集数据吗?也许我应该以不同的方式调用部分视图?

非常感谢.

Wah*_*tar 6

使用EditorTemplate而不是部分视图

@model Survey  

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    @Html.EditorFor(m=>m.Respondent) 
}
Run Code Online (Sandbox Code Playgroud)

并且不要忘记在Respondent.cshtml文件EditorTemplates夹中创建文件并在其中输入要编辑的Respondent字段.