asp.net-mvc ASP.NET MVC 返回空视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6857607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC return empty view
提问by SiberianGuy
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 中引用它......但可能有任何内置选项?
回答by archil
回答by Nipuna
You can return EmptyResultto return an empty view.
您可以返回EmptyResult以返回空视图。
public ActionResult Empty()
{
return new EmptyResult();
}
You can also just return null. ASP.NET will detect the return type nulland will return an EmptyResult for you.
你也可以直接返回null。ASP.NET 将检测返回类型null并为您返回一个 EmptyResult。
public ActionResult Empty()
{
return null;
}
See MSDN documentation for ActionResultfor list of ActionResult types you can return.
有关可以返回的 ActionResult 类型列表,请参阅ActionResult 的 MSDN 文档。
回答by Muhammad Adeel Zahid
if you want to return nothing you can do something like
如果你不想返回任何东西,你可以做类似的事情
if (!returnValue)
return Content("");
return View(RealView);

