开发者

How can I tell if my action is being called by RenderAction?

I have an action that could potentially be called via a normal link, in which case I'd return a View(), or it could also be 开发者_运维技巧called via AJAX or RenderAction (ie as a Child Action) in which case I'd return a PartialView().

Sorting out the AJAX part is easy - but how can I test if my action is being rendered as a Child Action?

Ideally, I'd like to be able to write code like this:

if (Request.IsAjaxRequest() || Request.IsChildAction())
    return PartialView();

return View();

Obviously the Request.IsChildAction() does not exist - is there something simlilar, or do I just need to create a special ChildAction that always returns a PartialView?


You were almost there:

public ActionResult Foo()
{
    if (Request.IsAjaxRequest() || ControllerContext.IsChildAction)
    {
        return PartialView();
    }
    return View();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜