[RESOLVED]inline editing with the webgrid – checkbox state change not captured

The checkbox is now properly displayed  (disabled) when the grid first displays, with the correct state as per my table.

In Edit mode I can change the state.

Save has the desired result in that all values are passed on to the method to receives the JSON. All changes are correctly recorded except BillMonthly. Request["BillMonthly"]
is always true. Regardless of the state I leave the checkbox in.

I am not seeing where I messed up. Anyone please?

grid.Column("BillMonthly",
                            style: "col2",
                            format: @<text>
                                <span class="display-mode">@Html.CheckBox("billmonthly-display", item.BillMonthly, new { disabled = "disabled" })</span>
                                @Html.CheckBox("BillMonthly", item.BillMonthly, new { @class = "edit-mode" })
                            </text>),
var billmonthly = tr.find('#BillMonthly').attr('checked') ? true : false;
BillMonthly: billmonthly
tr.find('#billmonthly-display').removeAttr("disabled").attr('checked', barn.BillMonthly).attr('disabled', true);

var billmonthly = tr.find('#BillMonthly').is(':checked');

Ok, that works! Thanks.

But why?

Here is your sample code

var hardback = tr.find('#Hardback').attr('checked') ? true : false;

Your code works too. What is the difference?

That is because Mike’s code, that you’re using and said works! is a function that would return a boolean type value. 

var billmonthly = tr.find('#BillMonthly').is(':checked');

This above code would return either true, or false. Don’t you see that little is method, that checks whether the selected element (in this case #BillMonthly) is having that property of being cheched or not. Once done, the billmonthly code would have either
true or false value indicating the state of the element (hopefully a checkbox or radio).

Where as the second statement is a statement to check the value of the attribute, and if the value after evaluating the value is true, return true otherwise false. This is known as a ternary operator, it checks for the expression and executes either side
of the : depending on the value returned.

var hardback = tr.find('#Hardback').attr('checked') ? true : false;

You should also find it interesting and worth noting, that the following code would work too, 

var hardback = tr.find('#Hardback').attr('checked');

Why? Because I have just removed the true : false which makes no sense. The second code line is just like saying, 

var myName = "Afzaal Ahmad Zeeshan";

if(myName == "Afzaal Ahmad Zeeshan") {
   Response.Write("Afzaal Ahmad Zeeshan");
}

You should always try to minimize the statements where you can. No need to compare the values where a bool value is returned. 

This is the code from the inline editing with the webgrid sample code on
www.mikesdotnetting.com

That sample code works too, out of the box. Just not translated into my application.

var hardback = tr.find('#Hardback').attr('checked') ? true : false;

wavemaster

That sample code works too, out of the box. Just not translated into my application.

If you are using jQuery 1.9.x or newer, attr(‘checked’) won’t work because it was deprecated. The sample on my site uses jquery 1.8.1. That’s why it works there. I’ll get round ot updating the example code at some stage.

Leave a Reply