问题描述
我有可能会通过正常的链路被称为一个动作,在这种情况下我会返回一个视图(),或者也可以通过AJAX或的RenderAction(即作为孩子的行动)呼吁在这种情况下,我倒是返回PartialView()。
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().
整理出AJAX部分很容易 - 但是我怎么能测试,如果我的行为被渲染为儿童行动
Sorting out the AJAX part is easy - but how can I test if my action is being rendered as a Child Action?
在理想情况下,我想能够写code是这样的:
Ideally, I'd like to be able to write code like this:
if (Request.IsAjaxRequest() || Request.IsChildAction())
return PartialView();
return View();
显然,Request.IsChildAction()不存在 - 是有什么simlilar,还是我只需要创建一个特殊的ChildAction总是返回PartialView
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?
推荐答案
您几乎有:
public ActionResult Foo()
{
if (Request.IsAjaxRequest() || ControllerContext.IsChildAction)
{
return PartialView();
}
return View();
}
这篇关于我怎样才能知道我的行为是由所谓的RenderAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!