iggyweb
iggyweb

Reputation: 2483

MVC Check if Model in View Has Records

I am struggling to determine if a Model passed to a View actually has any records.

The code below loops through the parent recordset and passes a parent parameter to a child recordset. I've tried if (Model.Content != null) but that doesn't seems to work, the code just thinks there are records, when actually there aren't.

Can somebody please review the code below and let me know what I am doing wrong?

<ul class="nav sf-menu clearfix">
    @foreach (var navigation in Model.Navigation)
    {
        if (Model.Content != null)
        {
            @Html.SubMenuLink(navigation.Title, navigation.Action, navigation.Controller)
            @Html.Raw("<ul>")
            foreach (var content in Model.Content.Where(c => c.NavigationId == navigation.Id))
            {
                if (string.IsNullOrEmpty(content.Url))
                {
                    if (string.IsNullOrEmpty(content.Content1))
                    {
                    }
                    else
                    {
                        @Html.MenuLink(content.Title, "Home/Article/" + content.Id + "/" + ToFriendlyUrl(content.Title), "Home");
                    }
                }
                else
                {
                    @Html.MenuLink(content.Title, content.Url, "Home");
                }
            }
            @Html.Raw("</ul>")
            @Html.Raw("</li>")
        }
        else
        {
            @Html.MenuLink(navigation.Title, navigation.Action, navigation.Controller)
        }
    }
</ul>

Any help would be much appreciated :-)

NavigationViewModel

namespace WebApplication1.Models
{
    public class NavigationViewModel
    {
        public List<Navigation> Navigation { get; set; }
        public List<Content> Content { get; set; }
    }
}

HomeController

public ActionResult Navigation()
{
    var navigationModel = new NavigationViewModel();
    navigationModel.Navigation = (from m in db.Navigations where (m.Main == true) orderby m.Position select m).ToList();
    navigationModel.Content = (from n in db.Contents where (n.Active == true) orderby n.Position select n).ToList();

    return View(navigationModel);
}

Upvotes: 1

Views: 20371

Answers (7)

Hamit YILDIRIM
Hamit YILDIRIM

Reputation: 4549

If model empty then return empty default content will give you what do you expected

if (!model.Any())
   return Content("");

Upvotes: 0

arman
arman

Reputation: 125

@if (!Model.Any())
{
    Response.Redirect("Create");
}

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

If Content property is of List or Array type then do like this:

if(Model.Content != null && Model.Content.Count > 0)
{
    //do something
} 

if its a IEnumerable of some type then:

if(Model.Content != null && Model.Content.Count() > 0)
{
   //do something
}

and if you are sure that Model.Content will not be passed null from action method, then you can use Any():

if(Model.Content.Any())
{  
    //do something 
}

Upvotes: 1

Tony
Tony

Reputation: 1307

You could just use the Linq Method Any()

if (Model.Content.Any())
{

}

Edit: After second look. That if statement may not be right Model.Content inside the loop will always execute the same way. Are you use it isnt something like navigation.Content.Any()

Upvotes: 1

sumith madhushan
sumith madhushan

Reputation: 218

Simply you can do something like this

In your C# class introduce new property named called Empty.

public class YourClass
{

    public bool Empty
    {
        get 
        { 
            return ( ColumnID== 0 )
        }
    }
}

Then in your Razor view you can use this Empty property for check weather model has values or not

@if (Model.Empty)
{
   @*what ever you want*@
}
else
{
   @*what ever you want*@
}

Upvotes: 1

iggyweb
iggyweb

Reputation: 2483

My issue was that the Model.Content was initially being passed all records to the view so records did actually exist before the Where clause foreach (var content in Model.Content.Where(c => c.NavigationId == navigation.Id)) to find related Content records based on NavigationID.

I have amended my code as follows:

<ul class="nav sf-menu clearfix">
    @foreach (var navigation in Model.Navigation)
    {
        int records = Model.Content.Count(c => c.NavigationId == navigation.Id);

        if (records > 0)
        {
            @Html.SubMenuLink(navigation.Title, navigation.Action, navigation.Controller)
            @Html.Raw("<ul>")
            foreach (var content in Model.Content.Where(c => c.NavigationId == navigation.Id))
            {
                if (string.IsNullOrEmpty(content.Url))
                {
                    if (string.IsNullOrEmpty(content.Content1))
                    {
                    }
                    else
                    {
                        @Html.MenuLink(content.Title, "Home/Article/" + content.Id + "/" + ToFriendlyUrl(content.Title), "Home");
                    }
                }
                else
                {
                    @Html.MenuLink(content.Title, content.Url, "Home");
                }
            }
            @Html.Raw("</ul>")
            @Html.Raw("</li>")
        }
        else
        {
            @Html.MenuLink(navigation.Title, navigation.Action, navigation.Controller)
        }
    }
</ul>

I'm not sure this is the most elegant or efficient way to achieve the end goal, any suggestions to make the code more efficient I am happy to listen.

Upvotes: 0

Murali Murugesan
Murali Murugesan

Reputation: 22619

It may have empty list items. Better check the Count property.

if (Model.Content != null && Model.Content.Count>0)

Assuming Model.Content may be the type of IList or Array

Upvotes: 3

Related Questions