如何使用复选框列制作MVC 3 Webgrid?

4im*_*ble 11 webgrid asp.net-mvc-3

下面的代码将动作链接插入其中一个Web网格列.

    @{
    View.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";

    var usersGrid = new WebGrid(source: Model,
        rowsPerPage: 40);
}
@usersGrid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
                columns: usersGrid.Columns(
                    usersGrid.Column(format: (item) => 
                         Html.ActionLink("Edit", "Edit", new { id = item.Id})),
                    usersGrid.Column("Surname")
        )
    )
Run Code Online (Sandbox Code Playgroud)

但如果我为此交换该行:

                usersGrid.Column(format: (item) => Html.CheckBox(item.Id)),
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

错误4'System.Web.Helpers.WebGrid.Column(string,string,System.Func,string,bool)'的最佳重载方法匹配具有一些无效参数.

我不太明白两者之间的区别..为什么一个工作而另一个错误?

最终目标是能够勾选多个复选框,然后发送打印信息.

4im*_*ble 23

这最终对我有用.

usersGrid.Column(header: "Print?", format: @<text><input name="Prints" 
      type="checkbox" value="@item.ID" /></text>),
Run Code Online (Sandbox Code Playgroud)

感谢Nick Harris,他在博客的评论中找到了答案:http: //www.nickharris.net/2010/10/a-first-look-at-the-asp-net-mvc-3-的WebGrid /

  • 好的提示,如果你有一个布尔值来检查它,你可以添加一个额外的条件以及usersGrid.Column(标题:"标题",格式:@ <文本> <输入名称="HasFormgivaren"type ="checkbox"value = "@ item.ID"@(item.IsChecked == 1?"Checked":null)/> </ text>), (4认同)

小智 5

这对我有用:

grid.Column("SiparisNo", "Seç", format: (item) => Html.CheckBox(String.Format("Secili_{0}", (int)item.SiparisNo), false, new { @style = "width:60px;" }))
Run Code Online (Sandbox Code Playgroud)