【问题标题】:How to use custom HTML Helper methods in MVC web grid如何在 MVC Web 网格中使用自定义 HTML Helper 方法
【发布时间】: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”的适用方法,但似乎有一个使用该名称的扩展方法。扩展方法不能动态调度。考虑强制转换动态参数或调用扩展方法而不使用扩展方法语法。

【问题讨论】:

    标签: c# model-view-controller


    【解决方案1】:

    不支持扩展方法中的动态类型。 这应该工作

    format: @<span class="label">@Html.ViewDPByID(((int)item.EmployeeID,"employee image", "avatar rounded-circle avatar-sm mr-3")</span>, style: "EmployeeID")),
    

    【讨论】:

    • 仍然有这个错误link@Paulo
    • 谢谢老兄,它工作正常。我喜欢这种格式:@&lt;span&gt;@Html.ViewDPByID((string)Convert.ToString(item.EmployeeID), "Image placeholder", "avatar rounded-circle avatar-sm mr-3")&lt;/span&gt;, style: "EmployeeID"),
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多