获取 ASP.net 函数中的 JsonResult

发布于 2024-10-15 07:12:03 字数 1225 浏览 2 评论 0原文

如何获取返回 JsonResult 并转换为字符串的方法的结果。

 [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 });
        }

我想这样调用我的方法

string MyValue = AnalyseNbPayment(Convert.ToDateTime(ViewModel.oRent.DateFrom), Convert.ToDateTime(ViewModel.oRent.DateTo), Convert.ToInt32(oLease.FrequencyID)).val.ToString(); <br />

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-10-22 07:12:03

试试这个:

var jsonResult = AnalyseNbPayment();
var json = new JavaScriptSerializer().Serialize(jsonResult.Data);

Try this:

var jsonResult = AnalyseNbPayment();
var json = new JavaScriptSerializer().Serialize(jsonResult.Data);
很快妥协 2024-10-22 07:12:03

另一种选择是使用动态类型来检索信息。在你的情况下,它会是(确保在分配的末尾有.Data):

dynamic MyValue = AnalyseNbPayment(Convert.ToDateTime(ViewModel.oRent.DateFrom), Convert.ToDateTime(ViewModel.oRent.DateTo), Convert.ToInt32(oLease.FrequencyID)).Data;

然后你可以简单地执行以下操作:

//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();

动态类型是一些疯狂的东西!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文