Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:23:10+00:00 2026-05-25T10:23:10+00:00

I have an entity called Lookup who has a complex Type Description with two

  • 0

I have an entity called Lookup who has a complex Type Description with two string English and French. There has arise a time where no value will be store into the lookup. Now other entities have lookups as properties so we could have foo.Lookup.Description.English for example.

I’m trying to use a Web Grid to display the information being selected.

Originally my controller looked like

    public ViewResult Index()
    {
        var foos = db.Foos;

        return View(foo.ToList());
    }

and my view looked like

@model IEnumerable<Foo>

@{
    ViewBag.Title = "Index";
}

<h2>Stay Manager</h2>

@{
    var grid = new WebGrid(Model, defaultSort: "sortMe", rowsPerPage: 3);
    grid.GetHtml(htmlAttributes: new { id = "DataTable" });
}

@grid.GetHtml(columns: grid.Columns(
        grid.Column("Lookup.Description.English", "Column Header")
))

My issue is that Lookup can at times be null and ill will get an error saying the the Column Lookup.Description.English does not exist.

I found a solution but not a very elegant one and was hoping that there was a better a way. My solution was to change my controller action to

    public ViewResult Index()
    {
        var foos = db.Foos;

        foreach (Foo currentFoo in Foos.Where(s => s.Lookup == null))
        {                
            Foo.Lookup = new Lookup();
            Foo.Lookup.Description.English = "";
            Foo.Lookup.Description.French = "";             
        }

        return View(foos.ToList());
    }

Any suggestions on how to get the Web Grid to work better with null-able complex types?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:23:10+00:00Added an answer on May 25, 2026 at 10:23 am

    Not at all that familair with webgrid, but would the following be a solution for you?
    I made the following simple model:

    public class Foo
    {
        public string Name { get; set; }
        public Lookup Lookup { get; set; }
    }
    
    public class Lookup
    {
        public string Name { get; set; }
        public Description Description { get; set; }
    }
    
    public class Description
    {
        public string English { get; set; }
        public string French { get; set; }
    }
    

    controller action (i don’t have a db, so I mocked some data):

    public ViewResult Index()
    {
        //var foos = db.Foos;
    
        var foos = new List<Foo>();
    
        foos.Add(new Foo { Name = "Foo1" });
        foos.Add(new Foo
        {
            Name = "Foo2",
            Lookup = new Lookup
            {
                Name = "Lookup2",
                Description = new Description
                {
                    English = "englishFoo2",
                    French = "frenchFoo2"
                }
            }
        });
        foos.Add(new Foo
        {
            Name = "Foo3",
            Lookup = new Lookup
            {
                Name = "Lookup3",
                Description = new Description
                {
                    //English = "englishFoo3",
                    French = "frenchFoo3"
                }
            }
        });
        foos.Add(new Foo { Name = "Foo4" });
        foos.Add(new Foo
        {
            Name = "Foo5",
            Lookup = new Lookup
            {
                Description = new Description
                {
                    English = "englishFoo5",
                    French = "frenchFoo5"
                }
            }
        });
        foos.Add(new Foo { 
            Name = "Foo6",
            Lookup = new Lookup
            {
                Name = "Lookup6"
            }
        }); 
    
    
        return View(foos);
    }
    

    So, I now have Foos with or without Lookups (with or without Description).

    The view is as follows:

    @model IEnumerable<Foo>
    
    @{
        var grid = new WebGrid(Model, defaultSort: "sortMe", rowsPerPage: 10);
        grid.GetHtml(htmlAttributes: new { id = "DataTable" });
    }
    @grid.GetHtml(
    columns: grid.Columns(
        grid.Column(
            columnName: "Name",
            header: "Foo"
        ),
        grid.Column(
            columnName: "Lookup.Name",
            header: "Lookup",
            format: @<span>@if (item.Lookup != null)
                           { @item.Lookup.Name }
            </span>
        ),
        grid.Column(
            columnName: "Lookup.Description.English",
            header: "Description.English",
            format: @<span>@if (item.Lookup != null && item.Lookup.Description != null)
                           { @item.Lookup.Description.English }
            </span>
        ),
        grid.Column(
            columnName: "Lookup.Description.French",
            header: "Description.French",
            format: @<span>@if (item.Lookup != null && item.Lookup.Description != null)
                           { @item.Lookup.Description.French }
            </span>
        )
    )
    )
    

    which works just fine for me (Asp.Net MVC 3), it procudes the following html:
    [snip]

    <tr>
        <td>Foo4</td>
        <td><span></span></td>
        <td><span></span></td>
        <td><span></span></td>
    </tr>
    <tr>
        <td>Foo5</td>
        <td><span></span></td>
        <td><span>englishFoo5 </span></td>
        <td><span>frenchFoo5 </span></td>
    </tr>
    <tr>
        <td>Foo6</td>
        <td><span>Lookup6</span></td>
        <td><span></span></td>
        <td><span></span></td>
    </tr>
    

    [/snip]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Entity called Keyword, and it has two rows: id comment_id foo_id
I have a linq Entity called Enquiry, which has a property: string DateSubmitted. I'm
I have an entity called ProdTransaction . I am saving two records of type
I have an abstract entity called Block which contains two attributes: column and order
I have an entity called Photo. A photo has a date field and I
I have entity called Customer and it has three properties: public class Customer {
I have an entity called Object, here is the yaml code: Entities\Object: type: entity
Example: I have an Entity called Car which is abstract . Then there are
I have an entity called Foo. It has a nullable many-to-one association to Bar.
I have an entity called Attachment where it has a to-many relationship with no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.