asp.net-mvcasp.net-mvc-4razorpartial-viewsasp.net-mvc-ajax

The partial view 'First.cshtml' was not found or no view engine supports the searched locations


I'm trying to call partial view in div on click. I've written this:

@Ajax.ActionLink("Second", "Second", new AjaxOptions()
{
    HttpMethod = "GET",
    UpdateTargetId = "partials",
    InsertionMode = InsertionMode.Replace
})

<div id="partials">
</div>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/5.2/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

In my Index.cshtml. my controller looks like this:

public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult Second()
        {

            return PartialView("Second.cshtml");
        }

but my error says that my partial view can't found in:

~/Views/switchPartial/Second.cshtml.aspx
~/Views/switchPartial/Second.cshtml.ascx
~/Views/Shared/Second.cshtml.aspx
~/Views/Shared/Second.cshtml.ascx
~/Views/switchPartial/Second.cshtml.cshtml
~/Views/switchPartial/Second.cshtml.vbhtml
~/Views/Shared/Second.cshtml.cshtml
~/Views/Shared/Second.cshtml.vbhtml

but I have it in switchPartial folder.

If I write @Html.Partial("Second") in my div it renders correctly my partial view.

What have I done wrong?


Solution

  • No need to include the file extension at the end of the partial name. Try:

    return PartialView("Second");
    

    By not including the file extension, each of the registered ViewEngines is given its opportunity to resolve the requested view (be in Second.cshtml, Second.vbhtml or Second.aspx).