2012-11-27 54 views
1

在ASP.NET MVC3中,我使用Webgrid來填充數據。我有一列要讓該列中的數據具有操作鏈接。我的表看起來像這樣在ASP.net MVC3中如何提供WebGrid中的鏈接

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

我想提供一個鏈接到該行中每個ViewDetails所以,我想傳遞一個值是RollNo的動作移動到所需action.As好。這是如何實現的。我嘗試過這樣的事情。

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")))); 

回答

2

這個蹩腳的語法開始:

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

謝謝Jan :)完整答案的格式爲:@ @ Html.ActionLink(「View Details」,「StudentDtls」,「Home」,new {id = item.RollNo},null) Aada

0

你可以只使用:

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

裏面的<text>塊,你可以通過使用變量item這樣訪問當前行HTML.Raw()

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