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

The Archive Base Latest Questions

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

I have a model and that model has a public List<string> Hour { get;

  • 0

I have a model and that model has a public List<string> Hour { get; set; }
and the constructor

public SendToList()
    {
        Hour = new List<string> { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };
    }

My question is why don’t I get a selected value for this

@Html.DropDownListFor(model => model.Hour, Model.Hour.Select( 
                x => new SelectListItem
                {
                    Text = x,
                    Value = x,
                    Selected = DateTime.Now.Hour == Convert.ToInt32(x)
                }
            ))

But I get a selected value here.

@Html.DropDownList("Model.Hour", Model.Hour.Select( 
                x => new SelectListItem
                {
                    Text = x,
                    Value = x,
                    Selected = DateTime.Now.Hour == Convert.ToInt32(x)
                }
            ))

What is the difference?

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

    Because you need to assign the selected value to your model.

    So I would recommend you the following approach. Let’s start with the view model:

    public class MyViewModel
    {
        // this will hold the selected value
        public string Hour { get; set; }
    
        public IEnumerable<SelectListItem> Hours 
        { 
            get
            {
                return Enumerable
                    .Range(0, 23)
                    .Select(x => new SelectListItem {
                        Value = x.ToString("00"),
                        Text = x.ToString("00")
                    });
            } 
        }
    }
    

    you could populate this view model inside the controller:

    public class HomeController: Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                // Set the Hour property to the desired value
                // you would like to bind to
                Hour = DateTime.Now.Hour.ToString("00")
            };
            return View(model);
        }
    }
    

    and in your view simply:

    @Html.DropDownListFor(
        x => x.Hour, 
        new SelectList(Model.Hours, "Value", "Text")
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My view model has the property: public IEnumerable<string> Names { get; set; } My
I have a model that has several properties. The properties can be primitive (String)
Specifically, I have a model that has a field like this pub_date = models.DateField(date
I have a Person model that has a foreign key relationship to Book ,
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
Let's assume I have a model called product. Let's assume that product has three
I have a class that has a Generic type G In my class model
Ok, I am new to JQuery, I have a modal that has a asp:Literal
If I have the following model; public List<RecommendedProduct> recommendations Then public class RecommendedProduct public
I have an object from my domain model that has a child object. How

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.