返回空的 ActionResult (对于子操作)的最自然的方式是什么?
public ActionResult TestAction(bool returnValue) { if (!returnValue) return View(EmptyView); return View(RealView); }
我看到的一个选项是创建一个空视图并在 EmptyView 上引用它... ... 但是可能有任何内置选项吗?
如果你不想返回任何东西,你可以这样做
if (!returnValue) return Content(""); return View(RealView);
返回 EmptyResult 类的实例
return new EmptyResult();
You can return 空结果 to return an empty view.
public ActionResult Empty() { return new EmptyResult(); }
你也可以返回 null.ASP.NET 将 检测返回类型 null并为您返回 EmptyResult。
null
public ActionResult Empty() { return null; }
See ActionResult 的 MSDN 文档 for list of ActionResult types you can return.