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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:42:17+00:00 2026-06-15T05:42:17+00:00

I have an ASP.NET MVC3 application. I have the following ViewModel : public class

  • 0

I have an ASP.NET MVC3 application. I have the following ViewModel:

public class MyViewModel
{
    public string Year { get; set; }
    public string Month {get; set; }

    public IEnumerable<SelectListItem> Years
    {
       get
       {
           return Enumerable.Range(2000, DateTime.Now.Year - 2000).Select(x => new SelectListItem
           {
               Value = x.ToString(),
               Text = x.ToString()
           });
       }
    } 
}

And the following View:

@model MyNamespace.MyViewModel

@Html.DropDownListFor(
        x => x.Year, 
    new SelectList(Model.Years, "Value", "Text"),
    "-- select year --"
)

@Html.DropDownListFor(
        x => x.Month, 
    Enumerable.Empty<SelectListItem>(),
    "-- select month --"
)

I fill the DropDownList for Month with a jQuery function that is triggered onchange from the DropDownList for Year, which works perfectly. When the form is posted to the server and then the view is rendered back I want to keep the values in the ‘Month’ DropDownList as it happens correctly for Year. Therefore I tried (besides the jQuery script):

public class MyViewModel
 {
    public string Year { get; set; }
    public string Month {get; set; }

    public IEnumerable<SelectListItem> Months
    {
        get
        {
            if(Year != null)
            {
               return Enumerable.Range(1, 12).Select(x => new SelectListItem
               {
                  Value = x.ToString(),
                  Text = x.ToString()
               });
            }
        }
    }

And in the View:

 @Html.DropDownListFor(
            x => x.Month, 
       new SelectList(Model.Months, "Value", "Text"),
        "-- select month --"
    )

I left Years out of the code to make things shorter. This code throws a NullReferenceException at the first run because the IEnumerable is empty. how can I achieve my goal?

  • 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-06-15T05:42:18+00:00Added an answer on June 15, 2026 at 5:42 am

    You have to repopulate the month list in your action.
    The POST is stateless is not like asp.net webforms.
    Just do something like this in your code:

    public ViewResult MyAction(MyViewModel model)
    {
       if (ModelState.IsValid)
       {
           //.. do your stuff ...
           //return redirect to index if everything went ok
       }
    
       //something went wrong return the model
       model.Years = new SelectList(/* code to populate years */, "Value", "Text", Model.Year);
       model.Months = new SelectList(/* code to populate months */, "Value", "Text",  Model.Month);
    }
    

    As a side note I prefer to use SelectList instead of IEnumerable<SelectListItem> beacuse I can set the default value in the controller (a better fit for the controller responsibility in my opinion) and not in the View.

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

Sidebar

Related Questions

I have following code in my asp.net MVC3 application: string msg = Beginning report
I am creating an ASP.NET MVC3 application using NHIBERNATE. I have a base class
I have the following WebGrid in my ASP.NET MVC3 test application. It displays a
This is for an ASP.NET MVC3 web application. In RegisterRoutes I have the following
I am developing an application in asp.net MVC3 and I have the following questions:
In my ASP.NET MVC3 application I have the following method that use EF entities
I have the following JMenu inside my asp.net mvc3 web application:- <ul id=jMenu> <li><a
I have an ASP.NET MVC3 application that I have built and it has two
I have a ASP.NET MVC3 application that handles time-consuming processes (copying a large file
I have written an ASP.NET MVC3 Web application which runs perfectly inside the Visual

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.