简体   繁体   中英

My partial view still calls/renders the _Layout.cshtml

i have a MVC5 asp.net c# application.

I start off rendering the home page.

Now, when my user clicks a button it calls a jquery function.

The aim of this function is to get the html contents of a 2nd page and repalce the 1st page contents with that without refreshing the whole page.

So, in my _Layout page I have this DIV:

<div id="mainContent">
    @RenderBody()
</div>

This will load my default page.

The jquery function in this default page is:

$("#divHomeBanner").click(function () {
    var url = '/Welcome2';
    $('#mainContent').load(url);
});

the code behind is:

[HttpGet]
[Route("~/Welcome2")]
public ActionResult Welcome2()
{
    return PartialView();
}

I test by loading the default page clicking a menu option clicking the button that will 'take me back' to the welcome page (which for this example is called Welcome2 and is a complete copy of Welcome).

what I have noticed is that all the resources are loaded up again aka the JavaScript libraries.

I thought the whole intent of using partials was to render the content changes via ajax?

Is this wrong or have i implemented it wrong?

Thanks

In your view which you have made partial add this statement on top of it:

@{
 Layout = null;
}

This will make sure that your view _Layout.cshtml or if you have master layout with some other name does not gets called.

Another suggestion for you is to use @Url.Action() helper for generating urls instead of using magic strings:

var url = '@Url.Action("Welcome2","ControllerName")';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM