ASP.NET MVC return empty view

asp.net Mvc

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

return instance of EmptyResult class

 return new EmptyResult();

Solution 2 - asp.net Mvc

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.

Solution 3 - asp.net Mvc

if you want to return nothing you can do something like

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

   return View(RealView);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSiberianGuyView Question on Stackoverflow
Solution 1 - asp.net MvcarchilView Answer on Stackoverflow
Solution 2 - asp.net MvcNipunaView Answer on Stackoverflow
Solution 3 - asp.net MvcMuhammad Adeel ZahidView Answer on Stackoverflow