amit kumar
amit kumar

Reputation: 95

Calling action method through AJAX throwing exception to return view

When I am doing AJAX request to controller, Action Method could not find the View which I am trying to return and throwing the exception as shown at end of the question.

Following is the AJAX method calling the Action Method:

  $.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;
            }
        });

Following is Action Method and it is called properly from AJAX request but while returning View it is throwing exception.

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

The view 'listbatch' or its master was not found or no view engine supports the searched locations. The following locations were searched:

    ~/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

Upvotes: 0

Views: 629

Answers (2)

tanveer ashraf
tanveer ashraf

Reputation: 58

I had the same problem and got a solution for that after spending 2 days. Instead of using window.location.href = '/Courses/listbatch/?BtchTimingid=' + data.ID + "&InsertRetId=" + data.secndid; I suggest you to use window.location.href = '@Html.Raw(Url.Action("listbatch","Courses",new{BtchTimingid=data.ID, InsertRetId = data.secndid}))

If you use it without @Html.Raw, your URL will have &amp instead of & for parameters, it shouldn't cause any problem but i don't know why it caused for me and got the same problem that you have i-e The view was not found... So use Html.Raw as well.

Upvotes: 1

user1023602
user1023602

Reputation:

Your view is missing.

 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) =========
 } 

The following JavaScript does not generate an Ajax request, its a normal GET.

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

So View(model) expects to find listbatch.cshtml but cannot (presumably because it is missing) and you get the error message.

You need to create /Views/Courses/listbatch.cshtml
Have a look in the Views folder, see if it is there...

Upvotes: 1

Related Questions