从Controller中将EditorTemplate作为局部视图返回

时间:2011-12-06 16:18:54

标签: .net asp.net-mvc asp.net-mvc-3 mvc-editor-templates

我想从我的控制器返回一个EditorTemplate作为Partial View。

我目前正在做:

public ActionResult Create([Bind(Prefix="Create")]CreateViewModel model)
{
    return PartialView("~/Views/Shared/EditorTemplates/Template.cshtml", model);
}

问题是,在我执行此操作后,Create_前缀远离我的视线。有没有办法将编辑器模板作为局部视图返回并保留前缀?

Index.cshtml     @model IndexViewModel

@using(Html.BeginForm("Create"))
{
    @Html.EditorFor(m => m.Create, "Template")

    <input type="submit" value="Save" />
}

我正在通过AJAX调用提交此表单。当我最初调用EditorFor时,所有字段的前缀都为Create_。但是,在我提交表单并返回此PartialView后,前缀将丢失。

1 个答案:

答案 0 :(得分:26)

由于未在主视图的上下文中调用模板,因此会丢失其上下文。您可以在这种情况下定义前缀,如下所示:

public ActionResult Create([Bind(Prefix="Create")]CreateViewModel model)
{
    ViewData.TemplateInfo.HtmlFieldPrefix = "Create";
    return PartialView("~/Views/Shared/EditorTemplates/Template.cshtml", model);
}
相关问题