Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - How to make a MVC 3 Webgrid with checkbox column?

The code below will insert an actionlink into one of the web grids' columns.

    @{
    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")
        )
    )

But if i exchange that line for this:

                usersGrid.Column(format: (item) => Html.CheckBox(item.Id)),

I get this error:

Error 4 The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string, string, System.Func, string, bool)' has some invalid arguments.

I don't quite understand the difference between the two.. why does one work and the other error?

The ultimate goal is to be able to tick a number of check boxes and then send to print their info.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is what worked for me in the end.

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

Got to give thanks to Nick Harris, answer found in the comments of his blog here: http://www.nickharris.net/2010/10/a-first-look-at-the-asp-net-mvc-3-webgrid/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...