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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s