19

In cshtml file, based on a condition, what's the best way to return an empty partialview ?

Right now I have:

@if(Model.Count() > 0)
{
  loooonng partial view block of markup code
}

How can I re-do it to look cleaner closer to this:

@if(Model.Count() == 0)
{
  render an empty partial view
}

  loooonng partial view block of markup code goes here   <- This will obviously get executed only if Model.Count() > 0

Thanks in advance !

7
  • You can try this return Content(""); May 7, 2012 at 17:25
  • How does the bottom code block look cleaner than the top? It looks like you're in a Razor template, so the best way to render empty content is to not render anything... which is what your top code block is doing if Model.Count() <= 0 May 7, 2012 at 17:50
  • Because I would like to avoid having multiple runaway nested If statements. codinghorror.com/blog/2006/01/flattening-arrow-code.html
    – InspiredBy
    May 7, 2012 at 17:59
  • Does, return new EmptyResult(), work from a partial? Never tried. May 7, 2012 at 18:07
  • Empty result would return "empty result" from a controller, not render partial view from within the page. I think what Asif suggested below is the way to go. I'll try it in a bit.
    – InspiredBy
    May 7, 2012 at 19:49

5 Answers 5

17

I have been using

return Content("");

and is working fine.

4
  • 1
    Not working for me. Is this a Controller ActionResult or something ?
    – InspiredBy
    May 7, 2012 at 19:56
  • 1
    Its is Controller ActionResult. You should handle it at controller level. May 8, 2012 at 4:28
  • If you're going to use return Content("") shouldn't you use ContentResult as the return type of the method? Nov 27, 2012 at 16:21
  • 14
    cannot convert ContentResult to PartialViewResult :/
    – Nick
    Aug 7, 2013 at 14:53
15

Not sure if you still need an answer, but I came across this problem and here's what I did in the view:

@if(Model.Count() == 0)
{
return; // a simple return will stop execution of the rest of the View
}

On a Controller level, I created a new class and returned it in my action:

public class EmptyPartialViewResult : PartialViewResult
{
    public override void ExecuteResult(ControllerContext context)
    {
    }
}
0
14

If you're returning a PartialViewResult I found that in the controller you can use

return default(PartialViewResult);

or

return null;

without any problems. The only consideration I can think is if you are using

var partialView = Html.Action("Action", "Controller");

in your view then you will need to check for null. Html.RenderAction appears to accept it no problem.

5

Use EmptyResult class:

return new EmptyResult();
1
  • 6
    This won't compile if the controller method return type is PartialViewResult Apr 11, 2016 at 20:39
1

A view should not decide if it should be empty or contain something. A view should be as "dumb" as possible, simply displaying the data from the model in a "fancy" way. It is up to the controller to decided if the output should be empty or contain some data to display. In other words, it is up to the controller to return an empty view or a non-empty view.

Solution

Create an empty view (empty *.cshtml file) under Views/Shared:

MVC_Project 
├── Views
    ├── Shared
        ├── _Empty.cshtml

Controller code:

public virtual PartialViewResult SomeAction()
{
    //some condition to determine if the view should be empty
    //maybe check if some properties of the model are null?
    if(returnEmptyView) 
        return PartialView("~/Views/Shared/_Empty.cshtml");

    return PartialView("~/Views/Something/NormalView.cshtml", model);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.