The below code might help you
Checkbox Group:
var hobby= [];
$.each($("input[name='Hobby']:checked"), function(){
hobby.push($(this).val());
});
console.log("My hobbies are: " + hobby.join(", "));
Radio Button Group
var gender = $("input[name='Gender']:checked").val();
console.log("Your are a - " + gender);
Your empObj as below.
var hobby= [];
$.each($("input[name='Hobby']:checked"), function(){
hobby.push($(this).val());
});
var empObj =
{
CustomerId: $('#CustomerId').val(),
Name: $('#Name').val(),
Country: $('#Country').val(),
Gender: $("input[name='Gender']:checked").val();,
Hobby: hobby,
};
In case of check boxes there can be multiple checked by user so you will need to get all those like:
$("input[name='Hobby']:checked").each(function(){
console.log($(this).val());
});
This might also work for you but not sure:
var checkedValue = $("input[name='Hobby']:checked").val();
-
Code for Redio Button Or Check Box as following, i also use partial view
<div class="form-group">
<label for="Gender">Gender</label><br />
Male
<input id="Male" name="Gender" type="radio" value="Male" />
Female
<input id="Female" name="Gender" type="radio" value="Female" />
</div>
<div class="form-group">
<label for="Hobby">Hobby</label></br>
<input type="checkbox" id="SHobby" name="Hobby" value="Singing">
<label for="Hobby">Singing</label>
<input type="checkbox" id="DHobby" name="Hobby" value="Dancing">
<label for="Hobby">Dancing</label>
<input type="checkbox" id="WHobby" name="Hobby" value="Writing">
<label for="Hobby">Writing</label>
i use add Function code as following , when i add data it take by Default Male for Gender and Dancing For Hobby
function Add() {
debugger;
}
var empObj = {
CustomerId: $('#CustomerId').val(),
Name: $('#Name').val(),
Country: $('#Country').val(),
Gender: $('#Male').val(),
Gender: $('#Female').val(),
Hobby: $('#SHobby').val(),
Hobby: $('#WHobby').val(),
Hobby: $('#DHobby').val()
};
$.ajax({
url: "/Home/Add",
data: JSON.stringify(empObj),
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
debugger;
loadData();
$('#myModal').modal('hide');
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
}
$(document).ready(function () {
loadData();
});
please help me to get proper selection value for Gender OR Hobby
While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please
It is very simple. You have to create back end model that will parse the empObj and you create your empObj as below. var hobby= []; $.each($(“input[name=’Hobby’]:checked”), function(){ hobby.push($(this).val()); }); var empObj = { CustomerId: $(‘#CustomerId’).val(), Name: $(‘#Name’).val(), Country: $(‘#Country’).val(), Gender: $(“input[name=’Gender’]:checked”).val();, Hobby: hobby, };