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.
JSON.stringify({ model: myObj}),returning when Before is checked?if ($("#Start").is(':checked')) { var data = { Start: "True" }} else { var data = { Before: "True" }}; $.post('@Url.Action("Manage", "Home")', data, function() {..});