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 227035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:33:54+00:00 2026-05-11T19:33:54+00:00

I am trying to understand something a bit better with being new to C#,

  • 0

I am trying to understand something a bit better with being new to C#, .NET 3.5 and MVC.

I am running through the MVC NerdDinner example and if you look at the ViewModel here: http://nerddinnerbook.s3.amazonaws.com/Part6.htm#highlighter_662935

You can see the Country list and how it gets populated, this seems to work fine but I tried to do a similar thing below using LINQ and I am having problems, with the SelectList approach even though it inherits from the IEnumerable interface.

I have got a task table with a foreign key to a status table. The below code gives me a NullReferenceException when I do a GET on a create action. I can see that an anonymous task object would not have a status set.. so I probably need to check for it, but I dont understand how this is not done for the NerdDinner example??

        public class TaskViewModel {
        // Properties

        public Task Task { get; private set; }
        public SelectList Status { get; private set; }

        // Constructor
        public TaskViewModel(Task task) {
            TaskRepository taskRepo = new TaskRepository();
            Task = task;
            Status = new SelectList(taskRepo.GetStatus(), Task.Status.description);
        }

    }

        //
        // GET: /Tasks/Create

        public ActionResult Create()
        {
            Task task = new Task();

            return View(new TaskViewModel(task));
        } 


//Code from TaskRepository 

        private TaskManagerDataContext db = new TaskManagerDataContext();

        public IQueryable<Status> GetStatus() {
            return from status in db.Status
                   orderby status.description
                   select status;
        }

I did another approach using LINQ for the type dropdown and the population of the drop down works but I am yet to test if it selects the correct value once a post is made and the details view is returned. I am also wondering whether this should some how be moved into my repository rather than have a class in my controller doing this sort of thing??

Here is the code:

//In TaskViewModel Class
        public IEnumerable<SelectListItem> Types { get; private set; }

//In TaskViewModel constructor

                IList<NPType> types = taskRepo.GetTypes().ToList();

            Types =
                from type in types
                select new SelectListItem {
                    Selected = (type.typeId == task.typeId),
                    Text = type.description,
                    Value = type.typeId.ToString()
                };

//The TaskForm partial View that is used for the Create action of the TaskController
            <p>
                <label for="type">type:</label>
                <%= Html.DropDownList("Type", Model.Types)%>
                <%= Html.ValidationMessage("type", "*") %>
            </p>
            <p>
                <label for="status">status:</label>
                <%= Html.DropDownList("Status", Model.Status)%>
                <%= Html.ValidationMessage("status", "*") %>
            </p>

and the TaskForm view inherits System.Web.Mvc.ViewUserControl

  • 1 1 Answer
  • 5 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-11T19:33:54+00:00Added an answer on May 11, 2026 at 7:33 pm

    My attempt at trying to understand this better.

        //controller code creating a select list in the viewmodel class.
    //taskRepo.GetStatus() returns an IQueryable<Status>
            Status = new SelectList(taskRepo.GetStatus(), Task.Status);
    
    //MVC Framework SelectList class constructor and ToEnumerable method
                public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)
                : base(items, dataValueField, dataTextField, ToEnumerable(selectedValue)) {
                SelectedValue = selectedValue;
            }
    
            private static IEnumerable ToEnumerable(object selectedValue) {
                return (selectedValue != null) ? new object[] { selectedValue } : null;
            }
    

    I can see that SelectList uses its base class of MultiSelectList and that constructor is here:

            public MultiSelectList(IEnumerable items, string dataValueField, string dataTextField, IEnumerable selectedValues) {
            if (items == null) {
                throw new ArgumentNullException("items");
            }
    
            Items = items;
            DataValueField = dataValueField;
            DataTextField = dataTextField;
            SelectedValues = selectedValues;
        }
    

    When I run this project the html is given as:

        <select id="Status" name="Status"><option>NPTaskManager.Models.Status</option>
    <option>NPTaskManager.Models.Status</option>
    <option>NPTaskManager.Models.Status</option>
    <option>NPTaskManager.Models.Status</option>
    </select>
    

    Which is to be expected.

    If I change the controller code to:

    Status = new SelectList(taskRepo.GetStatus(), Task.Status.statusId.ToString(), Task.Status.description);
    

    Then I get a NullReferenceException. Since this is not an ArgumentNullException It seems to me that the root of the exception is not the first SelectList argument. What I am trying to understand is how this all occurs?
    Is it because Task.Status needs to be added to Task in the create action of the controller?

    I will change this code to use the LINQ approach that I used for the task above, all I am trying to achieve now is some understanding of what is going on.

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

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
I've been trying to read and understand a bit more about better code and
Im trying to write a small class to better understand bit flags in c++.
Trying to understand something. I created a d:\svn\repository on my server. I committed folders
I was trying to understand something with pointers, so I wrote this code: #include
I'm trying to understand on a deeper level if I'm missing something as to
I am trying to understand the difference between this: if (isset($_POST['Submit'])) { //do something
I'm trying to understand REST. Under REST a GET must not trigger something transactional
While trying to do something a bit more complicated, I ran across a behavior
i am trying to understand this google social graph api a bit and i

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.