ASP。NET MVC返回空视图

时间:2022-12-12 11:21:52

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中引用它……但是有没有什么内置选项呢?

3 个解决方案

#1


195  

return instance of EmptyResult class

返回EmptyResult类的实例

 return new EmptyResult();

#2


12  

You can return EmptyResult to return an empty view.

您可以返回EmptyResult返回空视图。

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

You can also just return null. ASP.NET will detect the return type null and will return an EmptyResult for you.

还可以返回null。ASP。NET将检测返回类型null,并为您返回一个EmptyResult。

public ActionResult Empty()
{
    return null;
}

See MSDN documentation for ActionResult for list of ActionResult types you can return.

关于可以返回的ActionResult类型的列表,请参阅MSDN文档。

#3


8  

if you want to return nothing you can do something like

如果你想返回任何东西,你可以做一些类似的事情。

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

   return View(RealView);

#1


195  

return instance of EmptyResult class

返回EmptyResult类的实例

 return new EmptyResult();

#2


12  

You can return EmptyResult to return an empty view.

您可以返回EmptyResult返回空视图。

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

You can also just return null. ASP.NET will detect the return type null and will return an EmptyResult for you.

还可以返回null。ASP。NET将检测返回类型null,并为您返回一个EmptyResult。

public ActionResult Empty()
{
    return null;
}

See MSDN documentation for ActionResult for list of ActionResult types you can return.

关于可以返回的ActionResult类型的列表,请参阅MSDN文档。

#3


8  

if you want to return nothing you can do something like

如果你想返回任何东西,你可以做一些类似的事情。

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

   return View(RealView);