简体   繁体   English

在 ASP.net MVC3 中如何在 WebGrid 中提供链接

[英]In ASP.net MVC3 how to provide an link in the WebGrid

In ASP.NET MVC3 I am using a Webgrid to populate the data.在 ASP.NET MVC3 中,我使用 Webgrid 来填充数据。 I have a column in which I want the data in that column to have an action link.我有一列,我希望该列中的数据具有操作链接。 My Table looks like this我的表看起来像这样

Roll No | StudentName  |  Class |  Course  |         |
======================================================
110     |  XY          |   5    |   Science| ViewDetails

I want to provide a link to each ViewDetails in the row so to move to the desired action.As well as I want to pass a value that is RollNo to the action.我想提供指向行中每个 ViewDetails 的链接,以便移动到所需的操作。同时我想将一个值为 RollNo 的值传递给操作。 How this can be achieved.这是如何实现的。 I tried something like this.我试过这样的事情。

var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10);
    grid.Pager(WebGridPagerModes.NextPrevious);
    @grid.GetHtml(tableStyle: "grid", 
        htmlAttributes: new { id = "DataTable" }, 
        headerStyle: "grid-header", 
        footerStyle: "grid-header", 
        alternatingRowStyle: "grid-alternating-row", 
        selectedRowStyle: "grid-selected-row", 
        rowStyle: "grid-row-style", 
        columns: grid.Columns(
           grid.Column(columnName: "RollNo", header: "RollNo"), 
           grid.Column(columnName: "StudentName", header: "StudentName"), 
           grid.Column(columnName: "Class", header: "Class"), 
           grid.Column(columnName: "Course", header: "Course"),
           grid.Column(header: "",
                  format:item => 
                    new HtmlString(Html.ActionLink("ViewDetails","Home")))); 

Do it with this crappy syntax:用这个糟糕的语法来做:

grid.Column(
    header: "Link", 
    format: @<text>@Html.ActionLink("ViewDetails", "Home")</text>)

Inside the <text> block you can access the current line by using the variable item like this:<text>块中,您可以通过使用变量item来访问当前行,如下所示:

format: @<text>@Html.ActionLink(item.Text, "Home")</text>

You could just use HTML.Raw()你可以只使用 HTML.Raw()

grid.Column(header: "",
                  format:item => 
                    HTML.Raw(Html.ActionLink("ViewDetails","Home")))); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM