Using Html.RenderPartial() in ascx files

人走茶凉 提交于 2019-11-29 16:21:11

问题


I'm trying to use Html.RenderPartial in acsx file and I'm getting an error:

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'RenderPartial' 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

<a href="/projects/<%=project.Id %>">
  <% Html.Label("fdf"); %>
  <% Html.RenderPartial("ProjectName", Model.Id); %></a></li>
 <%} %>

However I've import neccessary namespaces, so it won't be error at

<% Html.Label("fdf"); %>

Are there any methods to use Html.RenderPartial in ascx file?


回答1:


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

<% Html.RenderPartial("ProjectName", (int)(Model.Id)); %>

Or any other datatype Id is.




回答2:


In case anyone else made the same mistake I did:

@Model MyViewModel

This will treat your model as dynamic

@model MyViewModel

This is a correctly strongly typed view. Note the lack of capitalisation!

Note, that this is Razor, unlike the original question.




回答3:


The only way i found to pass eg. an IEnumerable was to create a local variable and pass in this one. For Example @{ IEnumerable<Demo.Models.Friend> friends = Model.Friends; Html.RenderPartial("_FriendsList", friends); }

Html.RenderPartial("_FriendsList", (IEnumerable<Demo.Models.Friends>)(Model.Friends)); did not work!



来源:https://stackoverflow.com/questions/3822546/using-html-renderpartial-in-ascx-files

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