I have an action method which have two string arrays as parameters, i am posting this action method with ajax post method, i am having one problem, On controller i got both arrays data same
but i am making both arrays with different data(one contains code other contains name)
below is my code
public ActionResult SectionBook(string[] cs,string[] cname)
{
}
var CourseSection=new Array();
var CourseName=new Array();
$('a p-button').live('click', function () {
var schoolCourseId = $(this).attr('id');
CourseSection.push(schoolCourseId);
CourseName.push($(this).html().split("(")[0]);
});
$('#btnSubmit').live('click', function () {
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: '/MyController/SectionBook',
// dataType: 'json',
data: $.toJSON(CourseSection, CourseName),
success: function (result) {
window.location.href = '/MyController/SectionBooks'
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
},
async: false,
cache: false
});
});
i have debugged the JS code and both arrays are having different values here but in controller cs and cname contains same data
I think the problem is that the toJSON method only takes one parameter and ignores the rest. So by doing
$.toJSON(CourseSection, CourseName)
You are only creating a JSON object with the values of the first array passed in.
If you wrap the 2 arrays in and object however like this
and then convert the newObj object to JSON with
or
You should be able to retrieve the values from both arrays in the controller using JavascriptSerializer