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?
RenderPartial function to render Partial View in ASP.Net MVC Razor. The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the @Html. RenderPartial function in ASP.Net MVC Razor.
Partial function which renders the Partial View. The name of the View and the object of the CustomerModel class are passed to the @Html. Partial function. In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.
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
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
@model dynamic
to
@model YourNamespace.YourModelType
This has the advantage of working on every @Html.Partial
call you have in the view, and also gives you intellisense for the properties.
Can also be called as
@Html.Partial("_PartialView", (ModelClass)View.Data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With