binary binary - 1 year ago 131
jQuery Question

What is the best way to render PartialView to a Layout Page in MVC?

enter image description hereThere is a layout page in my

project and I want to render all the page contents (partial views) by clicking menu links (hyperlink) on the left side as shown below. There are some methods using
, etc. but I am not sure which approach meets my needs best. Is there any example regarding to this problem containing the
page and

Answer Source

You have to first wrap your body content in div and assign any id to it:

<div id="divContentArea">

Now in Script Menu click event:

$(".menuLinkItem").click(function (e) {
    var controllerName = $(this).attr('data-controllername');
    var actionName = $(this).attr('data-actionname'); 

    if (String(actionName).trim() == '') {
        return false;
    if (typeof (controllerName) == "undefined") {
        return false;

    var url = "/" + controllerName + "/" + actionName;

    //Open url in new tab with ctrl key press
    if (e.ctrlKey) {, '_blank');
        return false;

        url: url,
        type: 'POST',
        success: function (data) {
            var requestedUrl = String(this.url).replace(/[&?]X-Requested-With=XMLHttpRequest/i, "");
            if (typeof (requestedUrl) == "undefined" || requestedUrl == 'undefined') {
                requestedUrl = window.location.href;

            // if the url is the same, replace the state
            if (typeof (history.pushState) != "undefined") {
                if (window.location.href == requestedUrl) {
                    history.replaceState({ html: '' }, document.title, requestedUrl);
                else {
                    history.pushState({ html: '' }, document.title, requestedUrl);


        error: function (xhr) {



[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public PartialViewResult Index()
        if (HttpContext.Request.HttpMethod == "GET")
            return View();
            return PartialView();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download