Conditional page layout with ASP.net MVC
I would like to respond to a web request with a partial view if the request is an Ajax request. If its not an Ajax request I would like to wrap the partial in a开发者_如何学运维 layout and deliver a full page.
What's the best way to do this?
Cheers, Ian.
Something like this should work:
if(request.IsAjaxRequest()) {
return PartialView();
} else {
return View();
}
You can create two copies of the Controller method, one for HTTP GET and one for HTTP POST. If all your AJAX uses POST, that'd handle it.
[HttpGet]
public ActionResult Index()
{
// Do something
return View();
}
[HttpPost]
public PartialViewResult Index()
{
// Do something
return PartialView();
}
Alternatively, in the MVC3 Futures Library, there's an [AjaxOnly]
tag you can use to similar effect.
精彩评论