1

I am developing application in ASP.Net MVC and getting wrong value of checkbox in controller. I am using following code for showing checkbox in view:

@Html.CheckBox("ENABLED", new { @class = "checkSwitch", @checked = "checked" })

It's related property in model class is this

public Nullable<bool> ENABLED { get; set; }

I am using jquery plugin at here http://labs.lukepeters.me/iphone-checkbox-switch/ which display this checkbox like this:

enter image description here

html code generated for it when it's checked(Yes) is like this:

enter image description here

html code generated for it when it's unchecked(No) is like this:

enter image description here

Now issue is that when I open form with this checkbox initially checkbox displays properly and when I make chekcbox checked/unchecked only once then all works fine. Means if checkbox is checked initially and I make it unchecked, save data then it return unchecked(false) which is right. But suppose checkbox is checked initially and I make it unchecked, again I make is checked, save data this time instead checkbox is checked, it return unchecked(false). Also suppose checkbox is unchecked initially and I make it checked, again I make is unchecked, save data this time checkbox is unchecked(false). It really weird don't know what is problem.

Any help is really helpful. Thanks.....

4
  • This is not related to css Commented Oct 30, 2014 at 15:25
  • In both instances there is a Value of the checkbox set to "true". Is this being set by the switch javascript? Commented Oct 30, 2014 at 15:30
  • see what value is being sent in the POST Commented Oct 30, 2014 at 15:41
  • Thanks for reply all. @matt-dot-net I saw posted values in Request object in controller, suppose initially checkbox is unchecked and I checked it then value posted is ENABLED=true&ENABLED=false also my viewmodel get binded properly for ENABLED only for first change it's right. Here first value is for checkbox and second value is for hidden field. But suppose initially checkbox is unchecked and I checked it, again unchecked and checked it, even checkbox for ENABLED is checked it posts ENABLED=false. I am really frustrated and no idea why is this happening. Commented Oct 31, 2014 at 7:32

1 Answer 1

0

If you are using Model with strongly typed View, try change your Razor to:

@model YourModel
<!-- [...] Html Content [...] -->
@Html.CheckBoxFor(m => m.ENABLED, new { @class = "checkSwitch", @checked = "checked" })

Then, inside your controller, the action should be something like this:

[HttpPost]
public ActionResult PostMethod(YourModel model)
{
    var isChecked = model.ENABLED;
    // [...]
}

Or, you may try another approach as:

public ActionResult PostMethod(bool ENABLED) { /*[...]*/ }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.