Asp.Net MVC:在@Html:Grid中使用HTML标签

时间:2011-05-25 09:47:00

标签: asp.net-mvc asp.net-mvc-3

是否可以在@ Html.Grid中使用内联HTML(我正在尝试在其中一列中创建超链接标记)。这是一个例子:

@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>

欢迎任何帮助。

更新 - 根据archil的帖子,我的新代码变为:

@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>

2 个答案:

答案 0 :(得分:2)

看看templated razor delegates。你可以在那里进行链接生成

答案 1 :(得分:0)

您可以尝试打包“&lt; a&gt; ...&lt; / a&gt;”在“@&lt; text&gt; ...&lt; / text&gt;”中标签。 在你的情况下,它看起来像这样:

@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>