Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderPartial from different folder in RAZOR

I've been trying to convert my aspx pages to cshtml and having an issue with rendering partial pages from another folder.

What I used to do:

<% Html.RenderPartial("~/Views/Inquiry/InquiryList.ascx", Model.InquiryList.OrderBy("InquiryId", MvcContrib.Sorting.SortDirection.Descending));%>

I would think that the equivalent would be:

@Html.RenderPartial("~/Views/Inquiry/_InquiryList.cshtml", Model.InquiryList.OrderBy("InquiryId", MvcContrib.Sorting.SortDirection.Descending))

This is obviously not working, I am getting the following error.

CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Partial' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

How would I achieve this with using the Razor view engine?

like image 377
Allfocus Avatar asked Dec 24 '10 04:12

Allfocus


People also ask

How do I render a partial view in another view?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.

How do you call a partial view from another controller?

Another best way is to place Partial View inside shared folder & call same partial View from different controller using Shared Folder. And then call it from controller as mentioned above. That's it.

What is difference between partial and RenderPartial?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

What is the difference between RenderAction and RenderPartial?

RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.


2 Answers

The RenderPartial does not return a string or IHtmlString value. But does the rendering by calling Write in the Response.

You could use the Partial extension, this returns an MvcHtmlString

 @Html.Partial( ....

or

 @{ Html.RenderPartial(....);  }

If you really want RenderPartial

like image 191
GvS Avatar answered Nov 13 '22 04:11

GvS


The compiler cannot choose the correct method because your Model is dynamic. Change the call to:

@Html.RenderPartial("~/Views/Inquiry/_InquiryList.cshtml", (List<string>)Model.InquiryList)

Or to whatever data type InquiryList is.

like image 44
Scott Avatar answered Nov 13 '22 04:11

Scott