Pergunta

I would like to return an EditorTemplate from my controller as a Partial View.

I am currently doing:

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

The problem is that after I do this the Create_ prefix goes away from my view. Is there a way to return an editor template as a partial view and retain the prefix?

Index.cshtml @model IndexViewModel

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

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

I am submitting this form with an AJAX call. When I originally call EditorFor, all of the fields have a prefix of Create_. However, after I submit the form and return this PartialView, the prefix is lost.

Foi útil?

Solução

Since the template wasn't invoked in the context of the main view it loses its context. You could define the prefix in this case as follows:

public ActionResult Create([Bind(Prefix="Create")]CreateViewModel model)
{
    ViewData.TemplateInfo.HtmlFieldPrefix = "Create";
    return PartialView("~/Views/Shared/EditorTemplates/Template.cshtml", model);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top