How to use a custom column in a Mvccontrib Grid Model?

℡╲_俬逩灬. 提交于 2019-12-04 07:45:21

Try like this:

public class MyGridModel : GridModel<MyModel>
{
    public MyGridModel()
    {
        Column.For(x => x.UserId);
        Column.For(x => x.Name);
        Column.Custom(MyImage);
        Column.For(x => x.Score);
        RenderUsing(new HtmlTableGridRenderer<MyModel>());
    }

    private static IHtmlString MyImage(MyModel model)
    {
        var div = new TagBuilder("div");
        var img = new TagBuilder("img");
        var a = new TagBuilder("a");
        img.Attributes["src"] = model.ImageUrl;
        img.Attributes["alt"] = model.Name;
        a.Attributes["href"] = model.Link;
        a.SetInnerText(model.Name);

        div.InnerHtml = string.Format(
            "{0}{1}",
            img.ToString(TagRenderMode.SelfClosing),
            a.ToString()
        );

        return MvcHtmlString.Create(div.ToString());
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!