jquery - Load PartialView for AJAX and View for non-AJAX request -
i wanna implement in facebook:
- after left click on photo, loaded via ajax
- after middle click on scroll, loaded additional layout
for have view, loaded in controller in 2 different methods:
public actionresult overview() { return view("overview"); } public actionresult overviewpartialview() { return partialview("overview"); } and in jquery script looks this:
$(contentcontainer).load(_link + 'partialview'); my question is, there better way solve problem? have tried in _viewstart:
@{ layout = "~/views/shared/_layout.cshtml"; if (isajax) { layout = null; } } and in controller:
public actionresult index() { if (request.isajaxrequest()) return partialview(); return view(); } but in solutions had problem cache, after opening page layout, in ajax request page layout loaded.
you use single action:
public actionresult overview() { return view(); } and inside _viewstart.cshtml:
@{ layout = request.isajaxrequest() ? null : "~/views/shared/layout.cshtml"; } another possibility use following:
public actionresult overview() { if (request.isajaxrequest()) { return partialview(); } return view(); } then if want avoid caching problems use post request instead of get:
$.post(_link, function(result) { $(contentcontainer).html(result); }); or use $.ajax , specify cache: false append unique query string parameter avoid browsers caching:
$.ajax({ url: _link, type: 'get', cache: false, success: function(result) { $(contentcontainer).html(result); } });
Comments
Post a Comment