【问题标题】:Calling action method through AJAX throwing exception to return view通过AJAX抛出异常调用action方法返回视图
【发布时间】:2017-06-30 14:32:55
【问题描述】:

当我向控制器执行 AJAX 请求时,Action Method 找不到我试图返回的视图并抛出异常,如问题末尾所示。

下面是调用Action Method的AJAX方法:

  $.ajax({
            url: "/Courses/AddTime",
            data: { DayName: DayValue, StartTimeValue: 
                   starttimeval,EndTimeValue: EndtimeVal },
           dataType: "json",
            type: "POST",
            error: function () {
                alert(" An error occurred.");
            },
            success: function (data) {
                window.location.href = '/Courses/listbatch/?BtchTimingid=' + 
                data.ID + "&InsertRetId=" + data.secndid;
            }
        });

以下是Action Method,它是从AJAX请求中正确调用的,但是在返回View时抛出异常。

public ActionResult listbatch(string Search_name, int? page, int? 
BtchTimingid = 0, int InsertRetId=0)
    {
   /// There Have some code which is working perfect 


       if (Request.IsAjaxRequest())
            return PartialView("_BatchTimingList", model.pageCoursesList);
        else
             return View(model);

    }

Exception Screenshot

未找到视图“listbatch”或其主视图或没有视图引擎 支持搜索的位置。以下位置是 搜索:

    ~/Views/Courses/listbatch.aspx
    ~/Views/Courses/listbatch.ascx
    ~/Views/Shared/listbatch.aspx
    ~/Views/Shared/listbatch.ascx
    ~/Views/Courses/listbatch.cshtml
    ~/Views/Courses/listbatch.vbhtml
    ~/Views/Shared/listbatch.cshtml
    ~/Views/Shared/listbatch.vbhtml

【问题讨论】:

  • 您可以尝试重写问题吗?这里的英语真的很难听...
  • 通过 Json 方法调用 Action 并传递参数,但在调用 Action 时,我创建了同名的 View 问题是视图不是调用会产生错误。
  • @amitkumar 您的 AJAX 方法发布到 url:“/Courses/AddTime”,但您向我们展示了操作方法的代码:“listbatch”?最好发布正确的 AJAX 调用或正确的操作方法,以了解出了什么问题。
  • 当你想要重定向的时候,你到底为什么要进行 ajax 调用——这毫无意义。进行正常提交。错误很明显——您重定向到listbatch 并调用return View(model),它希望您有一个名为listbatch.cshtml 的视图。如果您想要不同的视图,请指定视图名称。

标签: asp.net-mvc model-view-controller


【解决方案1】:

我遇到了同样的问题,并在花了 2 天后得到了解决方案。 而不是使用 window.location.href = '/Courses/listbatch/?BtchTimingid=' + data.ID + "&InsertRetId=" + data.secndid; 我建议你使用 window.location.href = '@Html.Raw(Url.Action("listbatch","Courses",new{BtchTimingid=data.ID, InsertRetId = data.secndid}))

如果你在没有@Html.Raw 的情况下使用它,你的 URL 将有 &amp 而不是 & 作为参数,它应该不会导致任何问题,但我不知道为什么它会导致我遇到和你一样的问题即未找到视图...所以也使用 Html.Raw。

【讨论】:

  • 非常感谢,你的工作非常完美......你只是我无法表达我的话......3天后,现在你的解决方案将会成功......
  • 但是,哥们的一个问题是我不能传递参数,因为它说当前上下文中不存在数据...
【解决方案2】:

您的视图不见了。

 public ActionResult listbatch(string Search_name, int? page, int? BtchTimingid = 0, int InsertRetId=0)
 {
     if (Request.IsAjaxRequest())
         return PartialView("_BatchTimingList", model.pageCoursesList);
     else
         return View(model);    // <======= HERE (not an AJAX request) =========
 } 

以下 JavaScript 不会生成 Ajax 请求,它是一个普通的 GET。

 window.location.href = '/Courses/listbatch/?BtchTimingid=' + 
                        data.ID + "&InsertRetId=" + data.secndid;

所以View(model) 期望找到listbatch.cshtml 但找不到(可能是因为它丢失了)并且您会收到错误消息。

您需要创建/Views/Courses/listbatch.cshtml
查看 Views 文件夹,看看它是否在那里...

【讨论】:

  • public JsonResult AddTime(CoursesModel Model, string DayName, string StartTimeValue, string EndTimeValue) { var resultsss = new { ID = EditBatchTimingId, secndid = retID };返回 Json(resultsss,JsonRequestBehavior.AllowGet); }////////这就是Json请求
  • 嗨,上面我向你发送了通过控制器的 json 请求,然后我传递了 windows.location 值但我的视图页面没有打开它转到那个控制器但仍然得到错误跨度>
  • 是的。问题是您的视图丢失了。在/Views/Courses/listbatch.cshtml 创建一个视图,然后重试。
  • 老兄,我想你无法理解...我已经告诉过你 listbatch.cshtml 已经创建并且通过 Ajax 我正在调用该操作..
  • 检查位置和文件名的拼写。当您收到该错误消息时,这意味着该文件不存在。
猜你喜欢
  • 1970-01-01
  • 2012-09-23
  • 2010-12-06
  • 2013-09-09
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多