c# - MVC3 数据不是从部分 View 收集的

标签 c# asp.net-mvc-3 partial-views

我正在用 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 文件,并将要编辑的受访者字段放入其中。

关于c# - MVC3 数据不是从部分 View 收集的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13709394/

相关文章:

c# - 为什么 foreach 在从 ListView 中删除项目时起作用,而在 ListBox 中不起作用?

multithreading - 将 SharpArchitecture 的 NHibernateSession 与不同的线程结合使用

c# - MVC3 重定向到 ActionResult 的路由

ruby-on-rails - Rails 3.0 渲染部分时未定义的局部变量

asp.net-mvc - ASP.NET MVC 强类型部分 View ,给出无法加载类型错误

ruby-on-rails - 如何在具有不同别名 MIME 的 View 中使用部分?

c# - razor 如何将字节 [8] 转换为字符串?

c# - 当对象超出 C# 范围时?

c# - 在 Hangfire 中禁用 PreserveCultureAttribute

c# - TinyMCE.MVC.Jquery 不会显示?