SiberianGuy
SiberianGuy

Reputation: 25282

ASP.NET MVC return empty view

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?

Upvotes: 118

Views: 75168

Answers (3)

Nipuna
Nipuna

Reputation: 7006

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.

Upvotes: 22

archil
archil

Reputation: 39491

return instance of EmptyResult class

 return new EmptyResult();

Upvotes: 235

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

if you want to return nothing you can do something like

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

   return View(RealView);

Upvotes: 8

Related Questions