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 !
You can return EmptyResult to return an empty view. You can also just return null .
In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.
To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.
I have been using
return Content("");
and is working fine.
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)
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With