Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Html.RenderPartial() in ascx files

Tags:

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?

like image 987
takayoshi Avatar asked Sep 29 '10 14:09

takayoshi


People also ask

How do I use RenderPartial?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is HTML RenderPartial?

RenderPartial(HtmlHelper, String) Renders the specified partial view by using the specified HTML helper. RenderPartial(HtmlHelper, String, Object) Renders the specified partial view, passing it a copy of the current ViewDataDictionary object, but with the Model property set to the specified model.

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 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.

like image 122
GvS Avatar answered Sep 17 '22 19:09

GvS


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.

like image 32
StuartQ Avatar answered Sep 20 '22 19:09

StuartQ