26

I two database entities that i need to represent and i need to output them in a single page.

I have something like this

Views Def ViewA ViewB Test ViewC

I want to ViewC to display ViewA, which displays ViewB.

Right now i'm using something like this:

// View C
<!-- bla -->
<% Html.RenderPartial(Url.Content("../Definition/DefinitionDetails"), i); %>


// View A
<!-- bla -->
<% Html.RenderPartial(Url.Content("../Definition/DefinitionEditActions")); %>

Is there a better to do this? I find that linking with relative pathnames can burn you. Any tips?

Any chance I can make somehtiing like...

Html.RenderPartial("Definition","DefinitionDetails",i); ?

Thanks for the help

4 Answers 4

48

this is works for me!

@Html.Partial("~/Views/NewsFeeds/NewsFeedPartial.cshtml")
1
  • 1
    why when it is on other controller folder its necesary put *.cshtml?
    – angel
    Jun 24, 2015 at 0:55
35
+100

You can refer to Views with full paths, like:

Html.RenderPartial("~/Views/Definition/DefinitionDetails")

Even better, use the T4MVC library, which does the above and makes it (quasi-) strongly-typed. You can refer to any view from any controller or view. You use it like this:

Html.RenderPartial(MVC.Definition.Views.DefinitionDetails)

or

Html.RenderPartial(MVC.Definition.Views.DefinitionDetails, myModel)
5
  • 4
    I've found that you have to add the '.ascx' to the end of the path with the first method. (This might just be for a particular project setup)
    – andypaxo
    Mar 11, 2011 at 17:09
  • 2
    Just to specify, we're talking about the Html.RenderPartial overload in System.Web.Mvc.Html.RenderPartialExtenstions not MvcContrib.UI.InputBuilder.Views.HtmlExtensions nor Microsoft.Web.Mvc.Html.HtmlHelperExtensions. Sometimes Resharper gives you too many choices.
    – flipdoubt
    Jan 6, 2012 at 17:42
  • 3
    Yes I tried the first method will produce error, it supposed to have the file extension. So it should be Html.RenderPartial("~/Views/Controller/Action.ext") where ext can be aspx, ascx, cshtml or vbhtml.
    – CallMeLaNN
    Jul 11, 2012 at 15:42
  • This answer is not quiet right. If you supply a path you need to supply the extension as well. if you do not supply a path you must NOT supply an extension. Details answer added below to clarify the options. Feb 6, 2014 at 12:12
  • Yes @CallMeLaNN you are right first method gives an error "The partial view '~/Views/Definition/DefinitionDetails' was not found" and it can be removed by adding ext at the end
    – Dragon
    Jul 29, 2015 at 13:41
14

Just to clarify which options work exactly:

1) The extension of the view file is required if you supply a path.

2) If you do not supply a path, do not supply the extension.

The examples below assume cshtml files.

Use RenderPartial in a code block:

// This looks in default view folder, then shared, checking for .aspx, .cshtml etc
Html.RenderPartial("DefinitionDetails"); 

// This looks in specified path and requires the extension
Html.RenderPartial("~/Views/Definition/DefinitionDetails.cshtml");

Use Partial for inline Razor syntax:

// This looks in default view folder, then shared, checking for .aspx, .cshtml etc
@Html.Partial("DefinitionDetails")

// This looks in specified path and requires the extension
@Html.Partial("~/Views/Definition/DefinitionDetails.cshtml")

Note: Apparently RenderPartial is slightly faster than Partial, but I also expect fully pathed names will be a faster than letting MVC search for the file.

If you are producing partials in a loop (i.e. from a collection in your view model), it is likely you will need to pass through specific viewmodels:

e.g.

   @foreach (var group in orderedGroups)
   {
       Html.RenderPartial("~/Views/ControllerName/ViewName.cshtml", group);
   }

I just had to do all this on a project and found the marked answer a little misleading.

2
  • Can I use Html.RenderPartial("~/Views/Definition/DefinitionDetails.cshtml", model) in action method to generate string? Oct 12, 2019 at 13:38
  • @HimalayaGarg It is just a function. You can use any function that returns a string the same way :) Nov 25, 2019 at 18:50
1

Could you not copy the partials into the shared folder then just do:

<% Html.RenderPartial("DefinitionDetails", i); %> and

<% Html.RenderPartial("DefinitionEditActions"); %>

1
  • Seeing as you want to access the view from multiple controllers I would say so.
    – ridecar2
    May 26, 2010 at 18:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.