Returning an EditorTemplate as a PartialView in an Action Result
Asked Answered
A

3

24

I have a model similar to this:

public class myModel 
{
    public ClassA ObjectA {get; set;}
    public ClassB ObjectB {get; set;}
}

In my main view, I have tags similar to this:

<div id="section1">
    <%=Html.EditorFor(m => m.ObjectA)%>
</div>
<div id="section2">
    <%=Html.EditorFor(m => m.ObjectB)%>
</div>

ClassA and ClassB both have Editor templates defined.

I created some JavaScript that makes an Ajax call to reload the section1 div. I want the action method to return the editor for ObjectA, ClassA.ascx that is in the EditorTemplates folder.

I have the following in my Action method:

public ActionResult GetData(int input) 
{
    // Process input here and create modelData

    return PartialView("ClassA", modelData);
}

This gives an error because it cannot find the ClassA view.

My solution has been to create a PartialView in the Views folder called "GetData" and my return renders the GetData view. The GetData view has only one line of code:

<%=Html.RenderForModel()%>

This does work, but I was wondering if there was a way for an action method to return and editor template?

Apologetics answered 3/3, 2010 at 18:48 Comment(0)
I
24
return PartialView("~/EditorTemplates/ClassA.ascx", modelData);
Insectivore answered 3/3, 2010 at 18:53 Comment(3)
That doesn't work. It still cannot find the template. However, it appears that it only searched the location "~/EditorTemplates/ClassA.ascx", which I wasn't expecting.Apologetics
However, giving the full path from application root does work. For example, "~/Views/MyView/EditorTemplates/ClassA.ascx". I didn't know you could do this.Apologetics
Is there a way to have the view render the template itself? Say if modelData is of type ClassA?Poinciana
W
28

Bonus points for gift wrapping:

public class CustomControllerBase : Controller
{
    public PartialViewResult EditorFor<TModel>(TModel model)
    {
        return PartialView("EditorTemplates/" + typeof(TModel).Name, model);
    }

    public PartialViewResult DisplayFor<TModel>(TModel model)
    {
        return PartialView("DisplayTemplates/" + typeof(TModel).Name, model);
    }
}

Have the controller (called, say, MyController) inherit from CustomControllerBase, and then:

public ActionResult MyAction(int id)
{
    return EditorFor(new MyViewModel(id));
}

The code will be looking for "~/Views/MyController/EditorTemplates/MyViewModel.ascx".

Westlund answered 7/1, 2011 at 17:45 Comment(1)
Any way to attach the newly created object to the parent model's collection property?Poinciana
I
24
return PartialView("~/EditorTemplates/ClassA.ascx", modelData);
Insectivore answered 3/3, 2010 at 18:53 Comment(3)
That doesn't work. It still cannot find the template. However, it appears that it only searched the location "~/EditorTemplates/ClassA.ascx", which I wasn't expecting.Apologetics
However, giving the full path from application root does work. For example, "~/Views/MyView/EditorTemplates/ClassA.ascx". I didn't know you could do this.Apologetics
Is there a way to have the view render the template itself? Say if modelData is of type ClassA?Poinciana
O
2

this worked for me (mvc 4)

public ActionResult GetData(int input) 
{
    // Process input here and create modelData

    return PartialView("EditorTemplates/ClassA", modelData);
}
Octopus answered 28/5, 2015 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.