简体   繁体   English

ASP.NET MVC返回空视图

[英]ASP.NET MVC return empty view

What is the most natural way to return an empty ActionResult (for child action)? 返回空ActionResult(用于子操作)的最自然方式是什么?

public ActionResult TestAction(bool returnValue)
{
   if (!returnValue)
     return View(EmptyView);

   return View(RealView);
}

One option I can see is to create an empty view and reference it at EmptyView... but may be there is any built-in option? 我可以看到的一个选项是创建一个空视图并在EmptyView中引用它...但可能有任何内置选项?

返回EmptyResult类的实例

 return new EmptyResult();

You can return EmptyResult to return an empty view. 您可以返回EmptyResult以返回空视图。

public ActionResult Empty()
{
    return new EmptyResult();
}

You can also just return null . 您也可以返回null ASP.NET will detect the return type null and will return an EmptyResult for you . ASP.NET将检测返回类型null并为您返回EmptyResult

public ActionResult Empty()
{
    return null;
}

See MSDN documentation for ActionResult for list of ActionResult types you can return. 有关可以返回的ActionResult类型的列表,请参阅ActionResult的MSDN文档

if you want to return nothing you can do something like 如果你想什么都不回报,你可以做点什么

if (!returnValue)
     return Content("");

   return View(RealView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM