Skip to content Skip to sidebar Skip to footer

Get Correct Checkbox Value When Using Razor Asp.net Mvc

I am rendering my form using razor. I iterate over class .control_group that's inside a form and create objects that I need to send back to controller. My form has checkboxes and h

Solution 1:

It is by design, you can read about this here: asp.net mvc: why is Html.CheckBox generating an additional hidden input

I can suggest you while iterating the elements do the following

if the form has another element with the same name, and it is not check box, skip it.

this way you can just collect the correct fields.

I am most certainly sure that you can handle this with JQUERY, if not, post a JSFIDDLE so we can help you.


Solution 2:

Razor syntax always creates a hidden field for radio button & checkbox. You can change your :input selector to :input:checkbox to do your task.

var ajaxData = $('.control_group').map(function (i, group) {
 var data = {};
 $(group).find(':input:checkbox').each(function () {
      data[this.name] = this.value;
  });
  return data;
}).get();

ajaxData = JSON.stringify({ 'ajaxData': ajaxData });

console.log(ajaxData);

Post a Comment for "Get Correct Checkbox Value When Using Razor Asp.net Mvc"