English 中文(简体)
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.

最佳回答

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/

问题回答

this is working for me:

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

You have to beware of using extension methods (Html.*) with dynamics (item)... it doesn t work well in csharp. When you do the new {} projection or call ToString, it s no longer dynamic. Alternatively, you could cast: (object)item.Id.

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

this should work

The easiest way:

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

This error is occurring because the CheckBox call is not returning the same datatype that ActionLink is returning.

This is what you do. Do a message box call on the action link call and the check box call with same arguments, rap each inside the function call TypeName() and display the results in a msgbox for u to see. Also, do a .ToString in both as well, now, look at the results, it should tell you if there is a discrepancy between the datatypes returned, if you can, post the results, and I can tell you more. Let me know.

After a vigorous search I found an optimal solution, you can use this logic instead if you re finding difficult to use HTML helpers.

grid.Column(header: "", format: @<text><input name="chkBox" type="checkbox" value="@item.Id" @(item.Id == false ? null : "checked") /></text>)

grid.Column("ID", "Select", format: (item) => Html.CheckBox((string)item.ID, false, new { @style = "width:60px;" }), canSort: false)

This is working perfect.





相关问题
How to change the default sort order to descending?

How do I change the defaultSort of my webGrid to be in the opposite/descending order? If it were SQL, I d be adding a DESC somewhere. Here s my working line of code for an Ascending sort: var grid =...

MVC 3 Webgrid Column

I m working on a MVC 3 webgrid at the moment, in one of the columns I wish to have a button, I ve achieved this when putting the following code in the view. @grid.GetHtml(columns: grid....

How to hide header on MVC3 WebGrid

Is there some easy way to hide header for MVC3 WebGrid extension? Something like var grid = new WebGrid(Model, canSort:false, canPage:false, showHeader:false); I can probably set css style for ...

asp.net mvc 3 webgrid sorting remains ?sortdir=ASC

I m experimenting a bit with several grids in asp.net mvc. Microsoft has a grid too know in the prerelease of mvc 3 so I thought I ll try that one out. The basic functionality is quite easy to ...

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: ...

Mvc 3 texbox in webgrid (razor)

Simple Q:How do you I get the textbox to show the value. Code below fail on item.LastName @model List<Mvc2010_11_12.Models.Employee> @{ var grid = new WebGrid(source: Model,defaultSort: "...

ASP.NET MVC 3 WebGrid paging issue

My data access layer returns collection with rows for single page and total number of rows. Unfortunately WebGrid component does not allow to specify total number of rows or total number of pages (...

热门标签