asp.net mvc - determine when a partialview is going to be rendered as fullview... and hijack the rendering - MVC -


so have page this:

....

now updating comments div using ajax , all. now, if user has javascript disabled, actionresult "comments" still ends returning partialview except time replaces entire page instead of div "commentsdiv". ruins formatting of page, because masterpage gone. there lot of such scenarios throughout website.

can universally specify if partialview rendered full view, something!! (like maybe redirect dummy full-page masterpage referencing partialview). other approaches?

note can't "isajaxrequest",because first time page loads, won't ajax request, actionresult still supposed return partialview.

if have understood comment isajaxrequest, first time page loads, want full view, not partial... pretty canonical reason using isajaxrequest.

so need is:

if (request.isajaxrequest) {     return view(); } else {    return partialview("mypartial"); } 

the other scenario can think of using redirect, eg, if implementing post redirect pattern. in case, can override onresultexecuted method in controller store result of isajaxrequest in tempdata.

that way, when request hits server, can check variable in tempdata. if empty, "original" request, return full page. else, redirected request , original request ajax request, , can return partial view safely. ie:

write property in controller follows:

public bool imreallyanajaxrequest {         {         if (tempdata["imajax"] == null) return false;         if (tempdata.containskey("imajax"))         {             return (bool)tempdata["imajax"];         }         else if (request.isajaxrequest())         {             return true;         }         else         {             return false;         }     } } 

then, write onresultexecuted follows:

protected override void onresultexecuted(resultexecutedcontext filtercontext) {     if (filtercontext.result redirecttorouteresult)     {         tempdata[keyisajaxrequest] = request.isajaxrequest();     } } 

that way cover angles , can use imreallyanajaxrequest everywhere else , know work, whatever scenario. or rather, have used build base wizardcontroller transparent ajax being available or not. thing of beauty , dry, if packaged base controller.

however, speculate, question not clear.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -