How to make a MVC 3 Webgrid with checkbox column?
Asked Answered
F

10

11

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.

Fatimafatimah answered 29/11, 2010 at 18:14 Comment(2)
See that : #4195940Bridgman
This will help youAudile
F
24

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/

Fatimafatimah answered 1/12, 2010 at 11:45 Comment(1)
Good tip, if you have a boolean value to check it you can add an additional condition as well usersGrid.Column(header: "Header", format: @<text><input name="HasFormgivaren" type="checkbox" value="@item.ID" @(item.IsChecked == 1 ? "Checked" : null) /></text>),Communication
T
5

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;" }))
Technicality answered 11/10, 2011 at 13:48 Comment(0)
C
4

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.

Cedeno answered 30/11, 2010 at 23:22 Comment(3)
Thanks, i'll get the chance to try this later today.Fatimafatimah
Does this mean in vb it's handled better? just asking hehe :)Mercurialize
I'm no VB expert, but I think it's a non-issue for VB. I believe dynamics are just cast as objects there. Csharp does runtime binding of dynamics, but can't resolve extension methods at that time.Cedeno
P
2

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

this should work

Prostyle answered 30/9, 2011 at 9:56 Comment(0)
T
1

The easiest way:

usersGrid.Column(format: (item) => Html.CheckBox("Id"))
Twofaced answered 6/2, 2014 at 14:27 Comment(0)
M
0

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.

Mercurialize answered 30/11, 2010 at 10:9 Comment(4)
Thanks, i'll get the chance to try this later today.Fatimafatimah
@kohan ok, let me know how it goes when you do, am interested myself now lol cheers mateMercurialize
Don't think the problem was with the checkbox but the dynamic item (which is what chenriks was getting at i think). But anyway, i have sorted it now. Many thanks!Fatimafatimah
@kohan nevertheless, it was because of a datatype return discrepancy, right? did you try the TypeName and tostring methods to get at it or where you just returning the wrong datatypes to begin with?Mercurialize
D
0

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>)
Diego answered 11/9, 2017 at 13:6 Comment(0)
S
0

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

This is working perfect.

Stites answered 6/4, 2023 at 20:38 Comment(0)
D
-3

Try this

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

instead of

usersGrid.Column(format: (item) => Html.CheckBox(item.Id)),
Dugas answered 29/11, 2010 at 19:18 Comment(0)
O
-3

**

@{
  var grid = new WebGrid(source: Model.ToList(), canPage: true, canSort: true);
  grid.Pager (WebGridPagerModes.All);
}

**

<div id="g1">
    @grid.GetHtml(

    columns:grid.Columns
    (grid.Column(columnName:"paymentno",header:"PAYMENT NO"),

    grid.Column(columnName:"mname",header:"NAME"),

    grid.Column(columnName:"pamt",header:"AMOUNT"),

    grid.Column(header: "Header", format: @<text><input name="HasFormgivaren" type="checkbox" 
        value="@item.checkresult" @(item.checkresult == "True" ? "Checked" : null) /></text>)
    )
    )
</div>
Onto answered 5/3, 2017 at 8:6 Comment(1)
You should add some explanation tooNavigator

© 2022 - 2024 — McMap. All rights reserved.