0

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
Liam
  • 27,717
  • 28
  • 128
  • 190
amit kumar
  • 95
  • 1
  • 10
  • 1
    Can you try rewriting the question? The english is really hard to follow here... – Capn Jack Jun 30 '17 at 14:36
  • calling Action through Json Method and pass Parameter it but at the time of Action called i create same name of View problem is that view is not call that gives Error. – amit kumar Jun 30 '17 at 14:41
  • Phrasing of your question seems tougher than your problem... – Siva Gopal Jun 30 '17 at 15:16
  • You don't have any views in the location listed. Add a view? – Liam Jun 30 '17 at 15:26
  • 1
    @amitkumar Your AJAX method posting to url: "/Courses/AddTime", but you shown us code for action method: "listbatch"? Better post the correct AJAX call or correct action method, to understand what is going wrong. – Siva Gopal Jun 30 '17 at 15:27
  • 1
    Possible duplicate of [The view or its master was not found or no view engine supports the searched locations](https://stackoverflow.com/questions/18273416/the-view-or-its-master-was-not-found-or-no-view-engine-supports-the-searched-loc) – Liam Jun 30 '17 at 15:28
  • The success function does `window.location.href = '/Courses/listbatch/` @SivaGopal – Liam Jun 30 '17 at 15:31
  • 1
    Why in the world are you making an ajax call when you want to redirect - its just pointless. Make a normal submit. And the error is obvious - your making a redirect to `listbatch` and calling `return View(model)` which expect you to have a view named `listbatch.cshtml`. Specify the view name if you want a different view. –  Jul 01 '17 at 01:27

2 Answers2

1

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...

  • public JsonResult AddTime(CoursesModel Model, string DayName, string StartTimeValue, string EndTimeValue) { var resultsss = new { ID = EditBatchTimingId, secndid = retID }; return Json(resultsss,JsonRequestBehavior.AllowGet); }////////Thats the Json request – amit kumar Jun 30 '17 at 16:37
  • Hi, Above i send u that json request which is passing through controller and after that i pass that windows.location value but my view page is not open it go to that contoller but still get Error – amit kumar Jun 30 '17 at 16:53
  • Yes. The problem is your view is missing. Create a view at `/Views/Courses/listbatch.cshtml` and try again. –  Jul 01 '17 at 05:31
  • Dude, i think u cant understand...I already told u that listbatch.cshtml are already created and through Ajax i am calling that action.. – amit kumar Jul 01 '17 at 06:22
  • Check the location and filename spelling. When you get that error message, it means the file is not there. –  Jul 01 '17 at 06:53
1

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.

  • Thanx a Lot Buddy, U r just Awesome its Working Perfetcly...U just i cant express my word..After 3 days it will be success now from Your Solutions.... – amit kumar Jul 03 '17 at 05:23
  • But, Buddy one problem is that i can't pass parameter in that it says data does not exist in the current context... – amit kumar Jul 03 '17 at 05:28