Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC - Render a partial View From an Area

I've created an area that will handle our some generic things across all our development products, just as log ins, HTML helpers, etc. Inside the area, I have a partial view that I'm attempting to reference outside of the area. I've registered the area with

public class Routes : AreaRegistration
{
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Common_default",
            "Common/{controller}/{action}/{id}",
            new {
                controller = "Account",
                action = "Index",
                id = UrlParameter.Optional
            });
    }

    public override string AreaName
    {
        get { return "MvcCommons"; }
    }
}

And now in the regular project, I'm trying to reference a view in the MvcCommons area...

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>TestGrid</h2>

<% Html.RenderPartial("jQGridTable", ViewData.Model); %>

But I keep getting that the view isn't found. Before, while creating the MVC Commons project, I was getting view errors, but the errors told me that it looked in both the area folders and the default view folders. This time, I'm only getting the default folders. Is there any way to accomplish this?

Thanks everyone!

like image 220
DavidAndroidDev Avatar asked Dec 08 '10 16:12

DavidAndroidDev


People also ask

How do you render a partial view inside a view in MVC?

In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.

Is partial view in MVC renders a portion of view content?

Partial view in ASP.NET MVC is special view which renders a portion of view content. It is just like a user control of a web form application. Partial can be reusable in multiple views. It helps us to reduce code duplication.


2 Answers

I haven't actually had to do this, but at a guess I would assume you should use Html.RenderAction() instead, something like Html.RenderAction("action", "controller", new { area = "Area", model = ViewData.Model }).

model will have to be replaced with the name of the action's parameters, but that should work.

edit this will require a controller and view setup for each action though.

like image 137
KallDrexx Avatar answered Sep 21 '22 19:09

KallDrexx


An important thing to remember is that when using RenderPartial you use it in the context of the current action.

As your action isn't in an area it will only look in the View/ folder for the controller the action belongs to then the shared folder.

Any views you wish to share between areas and controllers and have available at the route should be the root View/Shared folder. Really if the view is callable as a partial like that there is probably little reason for it to belong to an area.

You can call into area when you want to render actions rather than partials - which then changes the context of the current action to the action you call into thereby allowing you to then return views within that area.

like image 24
Mark Avatar answered Sep 17 '22 19:09

Mark