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>

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

4 Comments

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.
format:(item)=> Html.Raw(item) , u will get html text for the item
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.
Oh, I've found the solution. I should have written item.{my property name}, just simple. Thanks for your help :D
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.