【发布时间】:2019-10-10 10:54:12
【问题描述】:
我必须在网络网格中动态显示图像。我使用自定义 HTML 助手来做到这一点,它在网络网格之外工作正常,但我不知道如何在网格内使用它。我使用下面的脚本:
public static class CustomHtmlHelpers
{
public static IHtmlString ViewDPByID(this HtmlHelper helper, string src, string alt, string cssclass)
{
TagBuilder tb = new TagBuilder("img");
if (File.Exists("~/Assets/images/EmpDB/" + src + ".png"))
tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute("~/Assets/images/EmpDB/" + src + ".png"));
else
tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute("~/Assets/images/angular.jpg"));
tb.Attributes.Add("alt", alt);
tb.Attributes.Add("class", cssclass);
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
}
}
@webGrid.Table(
htmlAttributes: new { @id = "WebGrid", @class = "table align-items-center table-flush table-hover table-sm" },
headerStyle: "thead-light",
columns: webGrid.Columns(
//RowID
webGrid.Column(header: "EmployeeID",
format: @<span class="label">@Html.ViewDPByID(item.EmployeeID,"employee image", "avatar rounded-circle avatar-sm mr-3")</span>, style: "EmployeeID")),
footer: @<table class="footer">
<tr>
</tr>
</table>)
我收到此错误: []
“HtmlHelper>”没有名为“ViewDPByID”的适用方法,但似乎有一个使用该名称的扩展方法。扩展方法不能动态调度。考虑强制转换动态参数或调用扩展方法而不使用扩展方法语法。
【问题讨论】: