Displaying a boolean as a checkbox within a Webgrid
Asked Answered
S

2

7

I am trying to use a Webgrid and I can get the data to display but the bool values are where I'm having problems. I would like to just display a checkbox that has the value of my bool. I have been able to get a checkbox within the grid but it is editable and does not show the value correctly.

grid.GetHtml(tableStyle: "table", alternatingRowStyle: "alternate", headerStyle: "header",
columns: grid.Columns(grid.Column(columnName: "string1", header: "String1", format: @<text>@item.string1</text>),
grid.Column(columnName: "bool1", header: "bool1", format: bool1? </text>),

If someone has done this before and could shed some light on how to properly display a bool as a checkbox that would be appreciated!

Stertorous answered 17/5, 2012 at 16:24 Comment(0)
V
16

Try this for the bool column:

grid.Column(columnName: "bool1", 
    header: "bool1", 
    format: (item) => @Html.Raw("<input type='checkbox' " + ((item.bool1==true) ? "checked" : "") + " disabled='disabled' />")
)
Viscosity answered 17/5, 2012 at 16:36 Comment(3)
This does show a checkbox but for some reason it does not properly display the value. I have set it like you show, and also tried to hard code true and false as the value but still no change in whether it shows checked or not. Not sure why the value is not working!Stertorous
@Stertorous ah sorry, my mistake. The attribute actually needs to be checked=checked, not checked=true. Please see my update above.Viscosity
yea that'll do it. Thanks also just found this works too format: @<input name="bool1" type="checkbox" value="@item.bool1" @(item.bool1 == true ? "Checked" : null) disabled="disabled" />)Stertorous
A
0

Below is working code for checkbox in mvc5.

grid.column("Active", header: "Assign",
  @if (@item.Active){<input type="checkbox" checked="checked"  spellcheck="true"/>}
  else{<input type="checkbox"  spellcheck="true" />} </text>) 
Anfractuosity answered 19/12, 2014 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.