BigBadDom
BigBadDom

Reputation: 219

Asp.Net MVC: using HTML tags in @Html:Grid

Is it possible to use inline HTML inside a @Html.Grid (I'm trying to create a hyperlink tag in one of the columns). Here is an example:

@model Example.Models.ExampleItems

@{
    ViewBag.Title = "Example Items";
}

<div id="ExampleContentInnerContainer">
<h2>Example Items</h2>

<div id="ExampleListGrid">
    <span class="ExampleGrid">
    <p>
        @Html.Grid(Model.ExampleList).Columns(c =>
        {
            c.For(a => a.Example.Mandatory == true ? "Yes" : "No").Named("Mandatory");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateRequested)).Named("Date Requested");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateDeadline)).Named("Deadline Date");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateReceived)).Named("Date Received");         
            c.For(a => a.Comment).Named("Comment");
            c.For(a => a.Name).Named("Checklist type");
            c.For(a => @:<a href="www.google.com" />);
        }
        )
    </p>
    </span>
</div>

Any help would be welcomed.

UPDATE - based on archil's post, my new code has become:

@model Example.Models.ExampleItems

@{
    ViewBag.Title = "Example Items";

    Func<dynamic, object> updateLink = @<a href="#?w=500" rel="Checklist_Popup" class="poplight">@item</a>;
}

<div id="ExampleContentInnerContainer">
<h2>Example Items</h2>

<div id="ExampleListGrid">
    <span class="ExampleGrid">
    <p>
        @Html.Grid(Model.ExampleList).Columns(c =>
        {
            c.For(a => a.Example.Mandatory == true ? "Yes" : "No").Named("Mandatory");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateRequested)).Named("Date Requested");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateDeadline)).Named("Deadline Date");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateReceived)).Named("Date Received");         
            c.For(a => a.Comment).Named("Comment");
            c.For(a => a.Name).Named("Checklist type");
            c.For(a => updateLink("Update"));        
        }
        )
    </p>
    </span>
</div>

Upvotes: 1

Views: 8663

Answers (2)

Tomasz Mikuś
Tomasz Mikuś

Reputation: 136

You could try by wrapping "<a>...</a>" in "@<text>...</text>" tag. In your case it would look like so:

@model Example.Models.ExampleItems

@{
    ViewBag.Title = "Example Items";
}

<div id="ExampleContentInnerContainer">
<h2>Example Items</h2>

<div id="ExampleListGrid">
    <span class="ExampleGrid">
    <p>
        @Html.Grid(Model.ExampleList).Columns(c =>
        {
            c.For(a => a.Example.Mandatory == true ? "Yes" : "No").Named("Mandatory");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateRequested)).Named("Date Requested");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateDeadline)).Named("Deadline Date");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateReceived)).Named("Date Received");         
            c.For(a => a.Comment).Named("Comment");
            c.For(a => a.Name).Named("Checklist type");
            c.For(a => @<text><a href="www.google.com" /></text>);
        }
        )
    </p>
    </span>
</div>

Upvotes: 0

archil
archil

Reputation: 39491

Take a look at templated razor delegates. There you could do your link generation

Upvotes: 2

Related Questions