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

  • Home
  • SEARCH
  • 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 1007233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:35:12+00:00 2026-05-16T08:35:12+00:00

Starting with MVC2, messing around with a simple db and just using the index

  • 0

Starting with MVC2, messing around with a simple db and just using the index view to display the items like:

in the controller:

    //
    // GET: /Equipment/

    public ActionResult Index()
    {
        return View(database.Artists.ToList());
    }

then the auto generated code in the view:

        <td> <%: item.ArtistID %> </td>
        <td> <%: item.GenreID %> </td>

etc etc

In my example, its potential that this data hasn’t been filled up, so it may be null. Now when I tried to load the view I will get a NullReferenceException. So where would the code for this belong ?

I guess you could have an if statement in the view, though surely this is not where the logic should go. Should I create a html helper which just returns empty string if the value is null?

edit:

Thanks for the help.

I have another query. What if say I’m developing a index browse type page. At the moment im displaying all items on the one page but there are like 2k items. Is there a tutorial or example of how to split this up? I guess my index browse controller could just take a integer range value from the browser, then I just say display next 100

  • 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-16T08:35:13+00:00Added an answer on May 16, 2026 at 8:35 am

    When you return the list of artists you need to check for null and return a New list of Artists.

    This is best accomplished, IMO, by returning what is called a ViewModel.

    Class MyFormViewModel
        List<Artist> artists {get; set;}
    

    then in your controller

    MyFormViewModel fvm = new MyFormViewModel();
    
    fvm.artists = database.Artists.ToList();
    if (fvm.artists == null) fvm.artists = new List<Artist>();
    return View(fvm)
    

    Then your view inherits from MyFormViewModel

    Then consider factoring out the logic in the cntroller that gets the artists and sets objects out to another layer

    EDIT

    The reason for the FormViewModel is because if you want to add other things to return to the view you simply extend the model making it very easy to add more partial views etc

    EDIT 2

    If you have a partial viwe call ArtistList which takes the full list of artists. It then simply itterates through the list of artists and renders another PV call say Artist which is given a single instance of artist.

    Then you could do a simple check within the Artist partial view for a null.

    Or you could check in the ArtistList partial view for a null record and render another PV called say NullArtist.

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

Sidebar

Related Questions

I'm just starting to get into ASP.NET MVC, and saw today that the Beta
Im starting with MVC2 and i have a simple question: If i have a
Starting a new project and would like to use one of the MVC framworks.
Just starting out in asp.net. Have just created a login.aspx page in my site
I am starting asp.net MVC2 by going through a few tutorials, but they are
I have an MVC2 app where I am starting to use the STE 's.
I am just starting on MVC so this should be an easy question to
I'm just starting with ASP.Net MVC 2 and might be doing something wrong. I
I'm just starting to learn Entity Framework 4, and am a bit confused about
Starting with an array with 10K values. I want to randomly get 1000 values

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.