无法将类型'System.Web.Mvc.RedirectToRouteResult'隐式转换为'System.Web.Mvc.JsonResult' [英] Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.JsonResult'

查看:576
本文介绍了无法将类型'System.Web.Mvc.RedirectToRouteResult'隐式转换为'System.Web.Mvc.JsonResult'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将动作从JsonResult重定向到ActionResult,但出现错误.我的错误是无法将类型'System.Web.Mvc.RedirectToRouteResult'隐式转换为'System.Web.Mvc.JsonResult'". 我的代码

How can i Redirect To Action from JsonResult to ActionResult but i m getting error. my error is "Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.JsonResult'". My Code

Json结果:

   public JsonResult AddTruckExpensesTransactionChild(string totaldays, string amount)
   {
        string Mess = objActive.Save();
        if (Mess == "1")
        {
            return RedirectToAction("GetTruckExpensesChild", new { id="", sid="" });
        }
        return Json(Mess, JsonRequestBehavior.AllowGet);
   }

ActionResult:

ActionResult:

    public ActionResult GetTruckExpensesChild(string id, string sid)
    {
        TruckExpensesTransactionClass Transaction = new TruckExpensesTransactionClass();
        if (sid != null)
        {                
            Transaction.TransactionChild = objActive.ShowTransactionChild(id, sid);
            return View(Transaction);
        }
        else
        {                
            return View(Transaction);
        }
    }

推荐答案

您需要使用基类 ActionResult ,以便您可以返回 View JSON 内容部分视图:

You need to use base class ActionResult so that you can return View,JSON,Content or Partial View:

public ActionResult AddTruckExpensesTransactionChild(string totaldays, string amount)
   {
        string Mess = objActive.Save();
        if (Mess == "1")
              return Json(new { Url = Url.Action("GetTruckExpensesChild", new { id = "", sid = "" }) });

        return Json(Mess, JsonRequestBehavior.AllowGet);
   }

但是,如果您通过ajax调用此操作,则必须通过javascript重定向到该操作,因为返回RedirectToAction会返回html以响应ajax而不是重定向.

But if you are calling this action via ajax you have to redirect to that action via javascript because returning RedirectToAction will return the html in response of ajax but not a redirect.

因此,您需要通过带有某些标志的json返回操作网址,并检查响应是否具有该标志,请通过jquery重定向到该网址.

So you need to return action url via json with some flag and check that if response has that flag, redirect to that url via jquery.

在Ajax调用成功中,检查其URL:

In Ajax call success check if its url:

success: function(result) {
          if(result.Url.length > 0)
{
        window.location.href=result.Url;
}

这篇关于无法将类型'System.Web.Mvc.RedirectToRouteResult'隐式转换为'System.Web.Mvc.JsonResult'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆