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

旧街凉风 提交于 2019-12-04 18:14:20

问题


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?


回答1:


You were almost there:

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


来源:https://stackoverflow.com/questions/7423214/how-can-i-tell-if-my-action-is-being-called-by-renderaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!