在Jqgrid的每一行中添加Html.ActionLink

时间:2014-12-12 09:58:42

标签: jquery asp.net-mvc jqgrid

我是一个显示学生数据的Jqgrid,我不想使用Jgrid工具栏按钮进行编辑或删除。

所以我想要其他类似

的列
  @Html.ActionLink("Edit", "Edit", new { id=item.StudentID }) |
  @Html.ActionLink("Details", "Details", new { id=item.StudentID }) |
  @Html.ActionLink("Delete", "Delete", new { id=item.StudentID })

这是我的StudentGridModel:

  public class StudentsGridModel
{
    public StudentsGridModel()
    {
        StudentsGrid = new JQGrid
        {
            Columns = new List<JQGridColumn>()
            {
                              new JQGridColumn { DataField = "StudentID",
                                                    PrimaryKey = true,
                                                    Editable = false,
                                                    HeaderText = "ID",
                                                    TextAlign = Jq.Grid.TextAlign.Center,
                                                    Width = 50},
                                  new JQGridColumn { DataField = "FullName", 
                                                    Editable = true,
                                                    HeaderText = "Full Name",
                                                    Width = 175
                                                    },
                                 new JQGridColumn { DataField = "BirthDate", 
                                                    Editable = true,
                                                    HeaderText = "Birth Date",
                                                    Width = 125 },
                                 new JQGridColumn { DataField = "Photo", 
                                                    Editable = true,
                                                    HeaderText = "Photo",
                                                    Width = 175
                                                    },
                                 new JQGridColumn { DataField = "Gender", 
                                                    Editable = true,
                                                    HeaderText = "Gender",
                                                    Width = 100
                                                    },
                                  new JQGridColumn { DataField = "Country", 
                                                    Editable = true,
                                                    HeaderText = "Country",
                                                    Width = 100
                                                    },
                                  new JQGridColumn { DataField = "MobileNumber", 
                                                    Editable = true,
                                                    HeaderText = "Mobile Number",
                                                    Width = 100
                                                    },
                                  new JQGridColumn { DataField = "Email", 
                                                    Editable = true,
                                                    HeaderText = "Email",
                                                    Width = 100
                                                    }
            },
            Height = Unit.Percentage(100),
            Width = Unit.Percentage(100)
        };
        StudentsGrid.ToolBarSettings.ShowRefreshButton = true;
    }
    public JQGrid StudentsGrid { get; set; }
}

教师控制员:

// GET: Teacher
    public ActionResult Index()
    {
        var gridModel = new StudentsGridModel();
        var grid = gridModel.StudentsGrid;
        SetUpGrid(grid);

        return View(gridModel);
    }
    private void SetUpGrid(JQGrid grid)
    {
        grid.ID = "StudentsGrid";
        grid.DataUrl = Url.Action("DataRequested");
        grid.SortSettings.AutoSortByPrimaryKey = false;
        grid.SortSettings.InitialSortColumn = "FullName";
        grid.SortSettings.InitialSortDirection = SortDirection.Asc;

        grid.ToolBarSettings.ShowEditButton = false;
        grid.ToolBarSettings.ShowAddButton = false;
        grid.ToolBarSettings.ShowDeleteButton = true;

        SetUpVirtualScrollingGrid(grid);
    }
    private void SetUpVirtualScrollingGrid(JQGrid grid)
    {
        grid.PagerSettings.ScrollBarPaging = true;
        grid.PagerSettings.PageSize = 20;
        grid.Height = System.Web.UI.WebControls.Unit.Pixel(400);
    }
    public JsonResult DataRequested()
    {
        StudentsGridModel gridModel = new StudentsGridModel();
        var data = from s in db.Students
                   select new
                   {
                       s.StudentID,
                       s.FullName,
                       s.BirthDate,
                       s.Photo,
                       s.Gender,
                       s.Country,
                       s.MobileNumber,
                       s.Email
                   };
        return gridModel.StudentsGrid.DataBind(data);
    }

1 个答案:

答案 0 :(得分:0)

你可以在控制器中形成你的字符串。如果你真的想在answer {{3}}中使用HtmlHelper在控制器内部。

但最好只用自己的href表单字符串。

表格栏:

new JQGridColumn
{
    DataField = "Edit",
    Editable = false,
    HeaderText = "Edit",
    Width = 100
}

向其添加字符串数据:

StudentsGridModel gridModel = new StudentsGridModel();
        var data = from s in db.Students
                   select new
                   {
                       s.StudentID,
                       s.FullName,
                       s.BirthDate,
                       s.Photo,
                       s.Gender,
                       s.Country,
                       s.MobileNumber,
                       s.Email,
                       Edit = String.Format(@"<a href='/Student/Edit/{0})'>Edit</a>", s.StudentID)
                   };

但是您应该明白,您必须拥有方法Student的控制器Edit才能返回View。