When I try to render a partial view whose model type is specified as:
@model dynamic
by using the following code:
@{Html.RenderPartial("PartialView", Model.UserProfile);}
I get the following exception:
'System.Web.Mvc.HtmlHelper<dynamic>' 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.
However, the same code in a .aspx file works flawlessly. Any thoughts?
I had the same problem & in my case this is what I did
and in Partial view
Instead of casting the model in the RenderPartial call, and since you're using razor, you can modify the first line in your view from
to
This has the advantage of working on every
@Html.Partial
call you have in the view, and also gives you intellisense for the properties.Here's a way to pass a dynamic object to a view (or partial view)
Add the following class anywhere in your solution (use System namespace, so its ready to use without having to add any references) -
When you send the model to the view, convert it to Expando :
Cheers
There's another reason that this can be thrown, even if you're not using dynamic/ExpandoObject. If you are doing a loop, like this:
In that case, the "var" instead of the type declaration will throw the same error, despite the fact that RootFolder is of type "Folder. By changing the var to the actual type, the problem goes away.
Just found the answer, it appears that the view where I was placing the RenderPartial code had a dynamic model, and thus, MVC couldn't choose the correct method to use. Casting the model in the RenderPartial call to the correct type fixed the issue.
source: Using Html.RenderPartial() in ascx files
Can also be called as