6

I have a grid like this:

WebGrid grid = new WebGrid(source: Model, rowsPerPage: 5, ajaxUpdateContainerId: "GridContainer");

Now, I want to display "MyContent" column as raw HTML. What should I do?

<div id="GridContainer">
    @grid.GetHtml(columns:
        grid.Columns(
            grid.Column(
                columnName: "MyContent",
                //Format: What should I put here?
            )
        )
    )
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Triet Doan
  • 11,455
  • 8
  • 36
  • 69

2 Answers2

6

Use

<div id="GridContainer">
    @grid.GetHtml(columns:
        grid.Columns(
            grid.Column(
                columnName: "MyContent",
                format: (item) =>
                    {
                       var links = Html.ActionLink("Edit",   "Edit",    new {id = item.PrimaryKey})  + " | " +
                                   Html.ActionLink("Details","Details", new { id = item.PrimaryKey}) + " | "+
                                   Html.ActionLink("Delete", "Delete",  new { id = item.PrimaryKey});

                       return Html.Raw(links);
                    }
                )
            )
        )

which renders as

<td>
    <a href="/Home/Edit/5">Edit</a> |
    <a href="/Home/Details/5">Details</a> |
    <a href="/Home/Delete/5">Delete</a>
</td>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
heads5150
  • 7,263
  • 3
  • 26
  • 34
  • Thanks for your help. But what if I just want to render all of its content as raw Html? It's just a paragraph with tag formatted. – Triet Doan Jan 15 '13 at 02:54
  • 2
    format:(item)=> Html.Raw(item) , u will get html text for the item – Ravi Gadag Jan 15 '13 at 06:01
  • No, what I get is `RuntimeBinderException` with message `The best overloaded method match for 'System.Web.Mvc.HtmlHelper.Raw(string)' has some invalid arguments`. I think `item` here is not a string. – Triet Doan Jan 15 '13 at 06:49
  • Oh, I've found the solution. I should have written `item.{my property name}`, just simple. Thanks for your help :D – Triet Doan Jan 15 '13 at 07:08
0

I think it is much cleaner to use the annotations in the model.

[GridColumn(Title="&nbsp;", SanitizeEnabled=false,EncodeEnabled=false)]
    public IHtmlString RateLink
    {
        get{
            return new HtmlString("<a href=\"" + BaseUrl + Id + "\" target=\"_blank\">Open</a>");
        }
    }

If you use only annotations your html only needs:

@Html.Grid(Model).AutoGenerateColumns()
acivic2nv
  • 99
  • 1
  • 4