I’m making an ajax call to Action in ASP.NET MVC framework. In Action string jsonModel is always getting as null. What I missing here? Can you please help?
FYI — I’m trying to keep the signature of the Action same as I’m using an existing action. It would be great if we could check the way we are sending Json data to Action and why following action is failing to get the Json string.
Ajax Call :
function send(SubmissionID, EntityOrganizationID) {
var user = $('#commentrecipients').val();
var comment = $('#Comment').val();
var scObj = {};
scObj["EntityOrganizationID"] = EntityOrganizationID;
scObj["SubmissionID"] = SubmissionID;
scObj["Comment"] = comment;
scObj["RecipientModels"] = user;
var jsonModel = JSON.stringify(scObj);
alert(jsonModel);
$.ajax({
url: "/Submission/SubmissionHeaderCommentAction",
dataType: "JSON", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
data: jsonModel,
async:false,
success: function (result) {
// notify the data source that the request succeeded
options.success(result);
},
error: function (result) {
// notify the data source that the request failed
options.error(result);
}
});
Action:
[AuditItem("SubmissionHeaderCommentAction")]
public ActionResult SubmissionHeaderCommentAction(string jsonModel) // jsonModel comes as null
{
//convert json to our poco model
dynamic data = JObject.Parse(jsonModel);
.........
return AdComplianceStatusResult(HttpStatusCode.OK, "Comment submitted successfully");
}
3
Answers
try it:
Action:
*** Uodated
I think you should change your concept to make things easier:
And simply return json:
fix your ajax
IMHO much better way to use viewModel
with this action
UPDATE
If you need to use existing action you can use this code ( it was tested in Visual Studio)
and use this ajax