Mark
Mark

Reputation: 4873

Controller to render all Partial Views

I'm very new to ASP.NET MVC4. I have an application that uses numerous partial views, currently I have a controller that I am using to return PartialView for each of them, makeing a very long file.

public PartialViewResult somePartial()
    {
        return PartialView("someParital");

    }
public PartialViewResult someOtherPartial()
    {
        return PartialView("someOtherParital");

    }

Is there a way to create a controller that would return PartialView for an entire folder/directory?

Thanks,

Upvotes: 0

Views: 377

Answers (2)

Dilip Langhanoja
Dilip Langhanoja

Reputation: 4525

You can return multiple partial View Using return Json Data as string of PartialView as describe here https://stackoverflow.com/a/18978036/2318354

return Json(new {  strPartial1 = RazorViewToString.RenderRazorViewToString(this, "someParital", model) ,  strPartial12 = RazorViewToString.RenderRazorViewToString(this, "someOtherParital", model2) });

Now instead of using UpdateTargetId On Success function do following way Ajax Request

@Ajax.ActionLink("Link-Name", "ActionName", "ControllerName", new AjaxOptions {OnSuccess = "scsFunction" }) 

on javaScript

function scsFunction(response) {
    var temphtml1 = response.strPartial1 ;
    var temphtml2 = response.strPartial2 ;

    $("#dvPartial1").html(temphtml1);
    $("#dvPartial2").html(temphtml2);

    }

This way you can update number of Partial View at One Action Call

Upvotes: 0

Fals
Fals

Reputation: 6839

You can render Partial Views directly into the markup using HTML Helper. The easiest way could be store every Partial View name into a List somewhere, and then use it to render every Partial View by name.

@{
    var myPartialViews = new string[] { "someParital", "someOtherParital" };
 }

@foreach(string partialView in myPartialViews)
{
    Html.Partial(partialView) //or Html.RenderPartial(partialView);
}

Upvotes: 1

Related Questions