MVC: how to render view via jQuery get method

In Razor format:

To avoid page get fully rendered with it’s inheriting layout page content

In _ViewStart.cshtml, change the following from:

@{
Layout = “~/Views/Shared/_Layout.cshtml”;
}

to (adding the following code in bold):

@{
Layout = Request.IsAjaxRequest() ? null : “~/Views/Shared/_Layout.cshtml”;
}

How to load view via jQuery get method

<div id=”targetPlaceholder”></div>

var url = ‘/Home/Index’;

$.get(url, function (data) {
$(‘#targetPlaceholder’).html(data);
});

This will be handy if you need to render different view into one page, or rendering your view data into your modal dialog, or rendering one view in another when the view is not a partial view.

How to handle Postback/ form submit?

to be continue…

Advertisements