Reputation: 1959
How can I get the result of a method that return a JsonResult and cast into string.
[HttpPost]
public JsonResult AnalyseNbPayment(DateTime dt, DateTime dt2,int FrequencyID) {
if (FrequencyID == ApplConfig.Frequency.WEEKLY) {
return Json(new { val = GetNbWeek(dt, dt2) });
}
else if (FrequencyID == ApplConfig.Frequency.MONTHLY) {
return Json(new { val =GetDateDiffInMonth(dt, dt2) });
}
else if (FrequencyID == ApplConfig.Frequency.QUARTELY) {
return Json(new { val = GetQuarterLy(dt, dt2) });
}
else if (FrequencyID == ApplConfig.Frequency.BI_MONTLY) {
return Json(new { val = GetBiMontlhy(dt, dt2) });
}
else if(FrequencyID == ApplConfig.Frequency.YEARLY)
{
return Json(new { val = GetNbYear(dt, dt2) });
}
return Json(new { val =0 });
}
I want to call my method like this
string MyValue = AnalyseNbPayment(Convert.ToDateTime(ViewModel.oRent.DateFrom), Convert.ToDateTime(ViewModel.oRent.DateTo), Convert.ToInt32(oLease.FrequencyID)).val.ToString(); <br />
Thanks
Upvotes: 1
Views: 339
Reputation: 7076
Another option is using the dynamic
type to retrieve the info. In your case it would be (make sure to have .Data at the end of the assignment):
dynamic MyValue = AnalyseNbPayment(Convert.ToDateTime(ViewModel.oRent.DateFrom), Convert.ToDateTime(ViewModel.oRent.DateTo), Convert.ToInt32(oLease.FrequencyID)).Data;
Then you could simply do the following:
//the var val will be the appropriate data type...in your case it looks like int so .ToString() will get you what you want.
var result = MyValue.val.ToString();
The dynamic type is some crazy stuff!
Upvotes: 0
Reputation: 6771
Try this:
var jsonResult = AnalyseNbPayment();
var json = new JavaScriptSerializer().Serialize(jsonResult.Data);
Upvotes: 2