开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜