3

I'm trying to load a partial view using jQuery. The partial view is being loaded from Contact.cshtml. However, in Chrome, I keep getting a 404 when trying to load partialViewName.cshtml.

I have the following folder structure:

/Views/Contact/Contact.cshtml

/Views/Contact/partialViewName.cshtml

$('#divname').load('partialViewName');

The URL I'm on is http://localhost/Contact/Index

Any ideas what I'm doing wrong?

4thSpace
  • 42,526
  • 93
  • 284
  • 462
  • 3
    `$('#divname').load(url);` where `url = '@Url.Action("actionName", "controllerName")';` –  Aug 24 '15 at 23:02

3 Answers3

6

The folder structure and the name of the .cshtml file is irrelevant. You should make an ajax call to controller action which returns the partial view you want.

The controller action method returning the partial view should look like following,

//
// GET: /SampleController/MyAction

[HttpGet]
public ActionResult MyAction()
{
   return PartialView("_MyPartial");
}

Then you need to make a call to this method,

$('#divname').load("/SampleController/MyAction");
emre nevayeshirazi
  • 18,455
  • 11
  • 60
  • 81
1

If the partial view is a Razor view, you need to create a controller action to deliver the partial view. The default routing expects /Controller/Action. It is not a file path reference.

BJ Safdie
  • 3,301
  • 22
  • 23
-1

because javascript doesn't work when partial view loads