javascript - Create JSON string containing a List object -
i posting json string controller method accepts object created. object has 2 properties: list of ints, , string. want create json string in js post controller via ajax. can't find way create list dynamically in json though.
here's want build json
var applicantids = []; var rejectionreason = $("#selectedrejectreason").val(); applicantids = $("#rejectids").val(); var rejection; jquery.each(applicantids, function(i) { //add applicantids rejection object }); //add rejectionreason rejection object rejectapplicants(rejection);
here's ajax post
function rejectapplicants(rejection) { jquery.ajax({ type: "post", url: '@url.action("rejectapplicants", "home")', data: json.stringify(rejection), datatype: "json", contenttype: "application/json", success: function (data) { if (data.redirectto == null) { window.location.href = '@url.action("error", "home")'; } else { alert("all selected applicants exported succesfully!"); window.location.href = data.redirectto; } }, error: function () { window.location.href = '@url.action("error", "home")'; } }); }
controller
[httppost] public actionresult rejectapplicants(rejection myrejection)
and rejection object
public class rejection { public list<int> applicantids { get; set; } public string rejectionreason { get; set; } }
you can declare object, this:
var rejection = { rejectionreason: $("#selectedrejectreason").val(), applicantids: $("#rejectids").val().split(',') }; rejectapplicants(rejection);
javascript jquery asp.net-mvc json
No comments:
Post a Comment