0

inside razor view I have two checbox properties. They are mutually exclusive. When one is selected other is not and vice versa.

@Html.EditorFor(model => model.Before)
@Html.EditorFor(model => model.Start)

inside same view on dom ready I manage checkbox click

$('#Before').change(function () {
     if ($(this).is(":checked")) {
        $('#Start').attr('checked', false);
        return;
     }   
     $('#Start').attr('checked', true);
});

$('#Start').change(function () {
    if ($(this).is(":checked")) {
        $('#Before').attr('checked', false);
        return;
    }
    $('#Before').attr('checked', true);
});

further I'm creating js object which will be stringified and sent to the mvc controller

var myObj= {            
      Before: $("#Before").is(':checked'),
      Start: $("#Start").is(':checked')      
};

 alert("Is before checked: "+$("#Before").is(':checked'));
 alert("Is start checked: "+$("#Start").is(':checked'));
/* This returns always selected (correct) value  */

$.ajax({
          type: 'POST',
          traditional: true,
          contentType: 'application/json',
          url: '/Home/Manage',
          data: JSON.stringify({ model: myObj}),              
          success: function (data) { }
          },error: function () {
              alert('error');
          }
});

and I have viewmodel which represent this myObj which is sent to the controller

public class MyViewModel
{
   public bool Start { get; set; }
   public bool Before{ get; set; }
}
[HttpPost]
public ActionResult Manage(MyViewModel model)
{ 
}

Problem is following:

When Start is checked then I'm getting Start as checked and Before as non checked which is fine, but when Before is checked and Start is not than both are received inside controller in model as false.

6
  • 3
    If they are mutually exclusive, why not have one property and use radio buttons to make a selection? Commented May 20, 2015 at 8:21
  • I know that should be natural option to choose, but not an option in this situation. Commented May 20, 2015 at 8:25
  • 1
    What is JSON.stringify({ model: myObj}), returning when Before is checked? Commented May 20, 2015 at 8:42
  • You could also simplify this using if ($("#Start").is(':checked')) { var data = { Start: "True" }} else { var data = { Before: "True" }}; $.post('@Url.Action("Manage", "Home")', data, function() {..}); Commented May 20, 2015 at 8:49
  • When you un/tick the checkboxes are they working correctly? Commented May 20, 2015 at 9:27

1 Answer 1

1

Try to use .prop('checked', true); instead of .attr('checked', true);

Sign up to request clarification or add additional context in comments.

3 Comments

nope, problem remains. again both are false in controller, when Before is checked.
Are you recreate your "myObj" each time before send?
Maybe instead of $("#Before").is(':checked') try $("#Before").prop('checked');

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.