在 Razor 视图引擎和 ASP.NET MVC 3 中使用动态模型渲染局部视图 [英] Render partial view with dynamic model in Razor view engine and ASP.NET MVC 3

查看:24
本文介绍了在 Razor 视图引擎和 ASP.NET MVC 3 中使用动态模型渲染局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试渲染模型类型指定为的局部视图时:

When I try to render a partial view whose model type is specified as:

@model dynamic

通过使用以下代码:

@{Html.RenderPartial("PartialView", Model.UserProfile);}

我收到以下异常:

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

但是,.aspx 文件中的相同代码可以完美运行.有什么想法吗?

However, the same code in a .aspx file works flawlessly. Any thoughts?

推荐答案

刚刚找到答案,看来我放置RenderPartial代码的视图有一个动态模型,因此MVC无法选择正确的方法使用.将 RenderPartial 调用中的模型转换为正确的类型解决了该问题.

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.

来源:在 ascx 文件中使用 Html.RenderPartial()

这篇关于在 Razor 视图引擎和 ASP.NET MVC 3 中使用动态模型渲染局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆