RenderPartial from different folder in RAZOR

吃可爱长大的小学妹 提交于 2019-12-22 03:23:37

问题


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?


回答1:


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




回答2:


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.




回答3:


Remember to include your strongly typed @model directive in your new Razor view. It is an easy step to miss when converting views from .aspx to .cshtml. If you forget, that 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Partial' error message could appear.



来源:https://stackoverflow.com/questions/4524267/renderpartial-from-different-folder-in-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!