117

What is the most natural way to return an empty ActionResult (for child action)?

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?

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266

3 Answers3

234

return instance of EmptyResult class

 return new EmptyResult();
archil
  • 39,013
  • 7
  • 65
  • 82
  • 1
    In an action that returns `EmptyResult`, is it the same as doing `return null`? – Robin Maben Aug 08 '12 at 13:08
  • 1
    @RobinMaben: No, null would not return an object from the method. EmptyResult however will. – cederlof Jun 17 '13 at 12:26
  • 1
    I'd return `null` because internally, it will use the `internal` `EmptyResult.Instance` that you cannot access yourself. This saves repeated instantiation of a stateless object. – Jorrit Schippers Nov 16 '16 at 16:16
22

You can return EmptyResult to return an empty view.

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.

public ActionResult Empty()
{
    return null;
}

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Nipuna
  • 6,846
  • 9
  • 64
  • 87
8

if you want to return nothing you can do something like

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

   return View(RealView);
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155