asp.net-mvc 在 ascx 文件中使用 Html.RenderPartial()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3822546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:31:06  来源:igfitidea点击:

Using Html.RenderPartial() in ascx files

asp.net-mvcpartial-viewsrenderpartial

提问by takayoshi

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

我正在尝试在 acsx 文件中使用 Html.RenderPartial,但出现错误:

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

编译器错误消息:CS1973:“System.Web.Mvc.HtmlHelper”没有名为“RenderPartial”的适用方法,但似乎具有该名称的扩展方法。扩展方法不能动态调度。考虑转换动态参数或调用没有扩展方法语法的扩展方法

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

是否有任何方法可以在 ascx 文件中使用 Html.RenderPartial?

回答by GvS

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

编译器无法选择正确的方法,因为您的模型是dynamic. 将调用更改为:

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

Or any other datatype Id is.

或者任何其他数据类型 Id 是。

回答by StuartQ

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.

请注意,与原始问题不同,这是 Razor。

回答by Muckman

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); }

我发现通过的唯一方法,例如。一个 IEnumerable 是创建一个局部变量并传入这个变量。例如 @{ IEnumerable<Demo.Models.Friend> friends = Model.Friends; Html.RenderPartial("_FriendsList", friends); }

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

Html.RenderPartial("_FriendsList", (IEnumerable<Demo.Models.Friends>)(Model.Friends));不工作!