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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:47:45+00:00 2026-05-28T01:47:45+00:00

My first MVC project and have ran into an issue which I hope someone

  • 0

My first MVC project and have ran into an issue which I hope someone can assist with.

Basically I have a DropDownListFor object which I want to populate with a list of available times for the user to pick from and store the selected item in an attribute for later consumption.

The below is producing a null value error so I’m missing something obvious, any help would be appreciated.

Here’s what I have:

Controller:

private MyModelObject m_model = new MyModelObject();
public ActionResult Index()
{
    var AvailTimes = new SelectList(new[]
    {
        new {Value="00:00",Text="12:00 AM"},
        new {Value="00:30",Text="12:30 AM"},
        new {Value="01:00",Text="1:00 AM"},
        new {Value="22:30",Text="10:30 PM"},
        new {Value="23:00",Text="11:00 PM"},
        new {Value="23:30",Text="11:30 PM"},                 
    }); 
    return View(m_model);
}

Model:

public class MyModelObject
{
    public string StartTime { get; set; }
    public IEnumerable<SelectList> AvailTimes { get; set; }
}

View:

@Html.DropDownListFor(model => model.StartTime, 
                       new SelectList(model.AvailTimes, "Value", "Text"))
  • 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-28T01:47:46+00:00Added an answer on May 28, 2026 at 1:47 am

    Don’t make your model a private variable to the controller. Each time a request is sent a new controller instance will be created so don’t think that you will be able to reuse it between actions. Also you don’t seem to be doing anything useful with the AvailTimes local variable that you declared in your action and it is subject to a fast garbage collection. Also your model is incorrect. The AvailTimes property must be an IEnumerable<SelectListItem> and not IEnumerable<SelectList>.

    Let’s start by fixing the view model first:

    public class MyModelObject
    {
        public string StartTime { get; set; }
        public IEnumerable<SelectListItem> AvailTimes { get; set; }
    }
    

    then the controller action which will feed the view model:

    public ActionResult Index()
    {
        var model = MyModelObject
        {
            AvailTimes = new[]
            {
                new SelectListItem { Value = "00:00", Text = "12:00 AM" },
                new SelectListItem { Value = "00:30", Text = "12:30 AM" },
                new SelectListItem { Value = "01:00", Text = "1:00 AM" },
                new SelectListItem { Value = "22:30", Text = "10:30 PM" },
                new SelectListItem { Value = "23:00", Text = "11:00 PM" },
                new SelectListItem { Value = "23:30", Text = "11:30 PM" },                 
             } 
        };
        return View(model);
    }
    

    and finally the strongly typed view:

    @model MyModelObject
    @Html.DropDownListFor(x => x.StartTime, Model.AvailTimes)
    

    You also have the possibility to assign those available hours in your view model directly:

    public class MyModelObject
    {
        public string StartTime { get; set; }
        public IEnumerable<SelectListItem> AvailTimes 
        { 
            get
            {
                return new[]
                {
                    new SelectListItem { Value = "00:00", Text = "12:00 AM" },
                    new SelectListItem { Value = "00:30", Text = "12:30 AM" },
                    new SelectListItem { Value = "01:00", Text = "1:00 AM" },
                    new SelectListItem { Value = "22:30", Text = "10:30 PM" },
                    new SelectListItem { Value = "23:00", Text = "11:00 PM" },
                    new SelectListItem { Value = "23:30", Text = "11:30 PM" },                 
                 }; 
            }
        }
    }
    

    Now your controller action could become:

    public ActionResult Index()
    {
        var model = MyModelObject();
        return View(model);
    }
    

    and the view obviously stays unchanged.

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

Sidebar

Related Questions

I have a new ASP.NET MVC project (my first), and I had been running
Im using MVC-Viewmodel, EF Model first on my project. I have a foreach loop
I am using MVC-Viewmodel, EF model first on my project. I have 3 DropDonLista
Does anyone have any basic instructions for setting up a first MVC project in
I'm currently working on my first MVC project which is nothing more than a
I have a form in an ASP.NET MVC project which has a select drop
I'm working on my first MVC project. I have very little knowledge on jscript,
I have a ASP.NET MVC project with a large list of input textboxes which
I have created a project in MVC 3 using code first and nugget ,
I have an ASPNET MVC 3 project made with Entity Framework 4 (database first).

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.