views:

46

answers:

1

Is there a clean way to manage my Asp.Net Mvc Web site to both work correctly if javascript is enabled/disabled. Because, for now, I have to do hack like that to make both work. I think that doesn't make code that is easy maintainable and reusable...

if (Request.IsAjaxRequest()) 
{                        
    return PartialView("SignUpForm", user);
}
else 
{
    return View("SignUp", user);
}
A: 

In this answer I've outlined a modal window technique that works cleanly without javascript; no code changes if you wanted to disable all the modal and javascript functionality.

http://stackoverflow.com/questions/1843894/simple-asp-net-mvc-crud-views-opening-closing-in-javascript-ui-dialog/1844916#1844916

The bits that I think are most important for you is custom ViewEngine:

public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
    //you might have to customize this bit
    if (controllerContext.HttpContext.Request.IsAjaxRequest())
        return base.FindView(controllerContext, viewName, "Modal", useCache);

    return base.FindView(controllerContext, viewName, "Site", useCache);
}

This code turns off the javascript and the surrounding template by loading a separate MasterPage if a request is from ajax or not. By switching the masterpage inside your own custom ViewEngine you avoid the if( Ajax ) code in all of your controllers and keeps thing clean.

jfar