I am working with .netcore asp.net project using C# jquery ajax json, I passed model from ajax call and received data(formdata) in string format but not be able to convert data of EmployeesList from string object to list object.
ajax code
$('#bttn_Click').click(function () {
debugger;
var empListVal = null;
empListVal = [];
$('input:checkbox:checked').each(function () {
empListVal .push($(this).attr('value'));
});
var Emp_des_ViewModel = {
Designation: des_Value,
Department: dep_Value,
EmployeesList: empListVal
};
$.ajax({
type: "post",
data: "formData=" + JSON.stringify(Emp_des_ViewModel),
url: '/Emp_Designation_Assign/InsertDesignation',
datatype: "json",
success: function (result) {
alert(result);
window.location.href = "/Emp_Designation_Assign/InsertDesignation";
console.log(result);
}
});
});
Emp_des_ViewModel.cs
public class Emp_des_ViewModel
{
public string Designation{ get; set; }
public string Department{ get; set; }
public List<SelectListItem> EmployeesList{ get; set; }
}
Emp_Designation_AssignController.cs
[HttpPost]
public IActionResult InsertDesignation(string formData)
{
var formdata = JsonConvert.DeserializeObject(formData);
Emp_des_ViewModel emp_desViewModel = new Emp_des_ViewModel();
emp_desViewModel = (Emp_des_ViewModel)formdata;
//other code
}