Answered by:
How to get JsonResults

Question
-
User-14642827 posted
I'm new to Json and I need to get results from another method (call another method) and Implement it (not sure what that means):
Being new to Json
public JsonResult CreateRequest(List<CertQuestionAnswer> certQuestionAnswers, List<Add_Facility> requestFacilities) { string returnValue = "success"; // how do you call this method? InsertCertificationQuestionarieAnswer(parameter1, parameter2, parameter3); // am I on the correct path? return Json(returnValue, JsonRequestBehavior.AllowGet); }
Monday, July 9, 2018 6:06 PM
Answers
-
User1520731567 posted
Hi bthJ6,
// how do you call this method? InsertCertificationQuestionarieAnswer(parameter1, parameter2, parameter3); // am I on the correct path?
According to your code, do you want to call a function and then change the returnValue according to this function result?
If so,you could add some if...else to judge the returnValue.
Just like:
public JsonResult CreateRequest(List<CertQuestionAnswer> certQuestionAnswers, List<Add_Facility> requestFacilities) { string returnValue = "success"; // how do you call this method? var flag=InsertCertificationQuestionarieAnswer(parameter1, parameter2, parameter3); // am I on the correct path? if(flag) { returnValue = "success"; } else { returnValue = "false"; } return Json(returnValue, JsonRequestBehavior.AllowGet); }
And then the result will return to the success callback function of the ajax:
$.ajax({ type: "GET", url: "/XXX/CreateRequest", dataType: "json", data: { ... }, success: function (result) { if(result.returnValue=="success")
{
//do something according to your need
}
else
{
//do something according to your need
} } });Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 10, 2018 7:17 AM
All replies
-
User-1171043462 posted
If you want to return string value then use ContentResult
public ContentResult CreateRequest(List<CertQuestionAnswer> certQuestionAnswers, List<Add_Facility> requestFacilities) { string returnValue = "success"; // how do you call this method? InsertCertificationQuestionarieAnswer(parameter1, parameter2, parameter3); // am I on the correct path? return Content(returnValue); }
Monday, July 9, 2018 6:25 PM -
User-369506445 posted
Hi
If your inner method return a value ,you can get it an put it instead of return value in last line
If your inner class does not return a value and it is a void ,it's better use a try catch block and finally return success or failed
public JsonResult CreateRequest(List<CertQuestionAnswer> certQuestionAnswers, List<Add_Facility> requestFacilities) { string returnValue = ""; // how do you call this method? try{
InsertCertificationQuestionarieAnswer(parameter1, parameter2, parameter3); // am I on the correct path? returnValue="success"; }catch{ returnValue=failed; } return Json(returnValue, JsonRequestBehavior.AllowGet); }Monday, July 9, 2018 7:20 PM -
User-369506445 posted
Also for more details you can follow below links
http://dotnetmentors.com/mvc/jquery-ajax-call-with-jsonresult-in-asp-net-mvc.aspx
Monday, July 9, 2018 7:33 PM -
User1520731567 posted
Hi bthJ6,
// how do you call this method? InsertCertificationQuestionarieAnswer(parameter1, parameter2, parameter3); // am I on the correct path?
According to your code, do you want to call a function and then change the returnValue according to this function result?
If so,you could add some if...else to judge the returnValue.
Just like:
public JsonResult CreateRequest(List<CertQuestionAnswer> certQuestionAnswers, List<Add_Facility> requestFacilities) { string returnValue = "success"; // how do you call this method? var flag=InsertCertificationQuestionarieAnswer(parameter1, parameter2, parameter3); // am I on the correct path? if(flag) { returnValue = "success"; } else { returnValue = "false"; } return Json(returnValue, JsonRequestBehavior.AllowGet); }
And then the result will return to the success callback function of the ajax:
$.ajax({ type: "GET", url: "/XXX/CreateRequest", dataType: "json", data: { ... }, success: function (result) { if(result.returnValue=="success")
{
//do something according to your need
}
else
{
//do something according to your need
} } });Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 10, 2018 7:17 AM