Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法隐式转换类型';System.Web.Mvc.redirectoroutesult';至';System.Web.Mvc.JsonResult';_C#_Asp.net Mvc_Json_Model View Controller_Asp.net Mvc 2 - Fatal编程技术网

C# 无法隐式转换类型';System.Web.Mvc.redirectoroutesult';至';System.Web.Mvc.JsonResult';

C# 无法隐式转换类型';System.Web.Mvc.redirectoroutesult';至';System.Web.Mvc.JsonResult';,c#,asp.net-mvc,json,model-view-controller,asp.net-mvc-2,C#,Asp.net Mvc,Json,Model View Controller,Asp.net Mvc 2,如何将操作从JsonResult重定向到ActionResult,但我遇到了错误。我的错误是“无法将类型“System.Web.Mvc.RedirectToRouteResult”隐式转换为“System.Web.Mvc.JsonResult”。 我的代码 Json结果: public JsonResult AddTruckExpensesTransactionChild(string totaldays, string amount) { string Mess =

如何将操作从JsonResult重定向到ActionResult,但我遇到了错误。我的错误是“无法将类型“System.Web.Mvc.RedirectToRouteResult”隐式转换为“System.Web.Mvc.JsonResult”。 我的代码

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);
   }
行动结果:

    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
类。只需将
JsonResult
更改为
ActionResult
,作为操作方法的返回类型:

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

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

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,而不是重定向

所以,您需要通过带有某个标志的json返回操作url,并检查若响应具有该标志,则通过jquery重定向到该url

在Ajax调用成功中,检查其url是否:

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

是的,此代码返回到该操作,但我的页面未重定向。您必须返回要通过jquery重定向和重定向的url。我无法理解您是否向我显示示例代码,因为我是jqueryNo Friend的新朋友,它不工作put警报(结果)要检查返回的内容是,此代码返回到该操作,但我的页面未重定向。