在 Razor 视图引擎和 ASP.NET MVC 3 中使用动态模型渲染部分
我有 MVC 辅助方法
public static string Selected<T>(this HtmlHelper helper, T value1, T value2)
{
if (value1.Equals(value2))
return "class=selected";
return String.Empty;
}
但是当我从视图中调用它
@Html.Selected(item.DomainID, Model.DomainID)>
或按照解释进行转换时 这里
@Html.Selected(item.DomainID, (int)(Model.DomainID))>
我的视图看起来像这样,
@model FYP.Models.ProjectIdea
@using Helpers
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<p class="validation-summary-errors">@ViewBag.Message</p>
@section LeftColumn{
<ul id="leftColumn">
@foreach (var item in ViewBag.Model.Domains)
{
<li @Html.Selected(item.DomainID, Model.DomainID)>
@Html.ActionLink(item.Title, "Index",
new { id = item.DomainID },
new
{
@id = "navLink:" + item.DomainID
}
)
</li>
}
</ul>
}
我收到以下错误。
编译器错误消息:CS1973:“System.Web.Mvc.HtmlHelper”没有名为“Selected”的适用方法,但似乎有一个使用该名称的扩展方法。扩展方法无法动态分派。考虑转换动态参数或在不使用扩展方法语法的情况下调用扩展方法。
有什么帮助吗?
更新:
好吧,我通过将参数类型更改为 int 来使其工作,
public static string Selected<T>(this HtmlHelper<T> helper, int value1, int value2)
{
if (value1.Equals(value2))
return "class=selected";
return String.Empty;
}
尽管这不再通用,但仍然解决了我的问题
I have MVC helper method
public static string Selected<T>(this HtmlHelper helper, T value1, T value2)
{
if (value1.Equals(value2))
return "class=selected";
return String.Empty;
}
But when I call it from view
@Html.Selected(item.DomainID, Model.DomainID)>
or cast it as explained here
@Html.Selected(item.DomainID, (int)(Model.DomainID))>
My view looks like this
@model FYP.Models.ProjectIdea
@using Helpers
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<p class="validation-summary-errors">@ViewBag.Message</p>
@section LeftColumn{
<ul id="leftColumn">
@foreach (var item in ViewBag.Model.Domains)
{
<li @Html.Selected(item.DomainID, Model.DomainID)>
@Html.ActionLink(item.Title, "Index",
new { id = item.DomainID },
new
{
@id = "navLink:" + item.DomainID
}
)
</li>
}
</ul>
}
I get the following error.
Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Selected' 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.
Any help?
Update:
Ok I got it working by changing the argument types to int
public static string Selected<T>(this HtmlHelper<T> helper, int value1, int value2)
{
if (value1.Equals(value2))
return "class=selected";
return String.Empty;
}
Though this is not generic any more bust still solved my problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
HtmlHelper
类型更改为通用类型(注意通用T
)...Try changing the
HtmlHelper
type to a generic one (note the genericT
)...