如何使用一个Mvccontrib网格模型的自定义列? [英] How to use a custom column in a Mvccontrib Grid Model?

查看:133
本文介绍了如何使用一个Mvccontrib网格模型的自定义列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用像这样一个ASP.NET MVC 3 Mvccontrib格:

I am using a ASP.NET MVC 3 Mvccontrib grid like so:

@Html.Grid(Model).Columns(column =>
{
  column.For(x => x.UserId).Named("ID");
  column.For(x => x.Name);
  column.Custom(@<div><img src='@item.ImageUrl' alt="@item.Name"/><a href="@item.Link">@item.Name</a></div>).Named("Name");
  column.For(x => x.Score).Named("Score");
})

但现在我需要这种移动到一个自定义网格模型:

But now I need to move this into a custom grid model:

@Html.MvcContrib().Grid(Model).WithModel(new MyGridModel()).Sort(ViewData["sort"] as GridSortOptions).Attributes(id => "grid", style => "width: 100%;")

与相应的网格模型:

with corresponding grid model:

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

不过,我该怎么办我的自定义列在我的网格模型?结果
Column.Custom(???);

But how can I do my custom column in my grid model?
Column.Custom(???);

推荐答案

尝试这样的:

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

这篇关于如何使用一个Mvccontrib网格模型的自定义列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆