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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:05:04+00:00 2026-05-14T15:05:04+00:00

Okay i’m new to asp mvc2 and i’m experiencing some problems with the htmlhelper

  • 0

Okay i’m new to asp mvc2 and i’m experiencing some problems with the
htmlhelper called Html.dropdownlistfor();

I want to present the user a list of days in the week. And i want the selected item to be bound to my model.

I have created this little class to generate a list of days + a short notation which i will use to store it in the database.

public static class days
{
    public static List<Day> getDayList()
    {
        List<Day> daylist = new List<Day>();

        daylist.Add(new Day("Monday", "MO"));
        daylist.Add(new Day("Tuesday", "TU"));
        // I left the other days out
        return daylist;
    }

    public class Dag{
        public string DayName{ get; set; }
        public string DayShortName { get; set; }

        public Dag(string name, string shortname)
        {
            this.DayName= name;
            this.DayShortName = shortname;
        }
    }
}

I really have now idea if this is the correct way to do it

Then i putted this in my controller:

SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
        ViewData["days"] = _list;
        return View("");

I have this line in my model

public string ChosenDay { get; set; }

And this in my view to display the list:

<div class="editor-field">
            <%: Html.DropDownListFor(model => model.ChosenDay, ViewData["days"] as SelectList, "--choose Day--")%>
        </div>

Now this all works perfect. On the first visit, But then when i’m doing a [HttpPost]
Which looks like the following:

[HttpPost]
    public ActionResult Registreer(EventRegistreerViewModel model)
    {
       // I removed some unrelated code here 

       // The code below executes when modelstate.isvalid == false
        SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
        ViewData["days"] = _list;
        return View(model);
    }

Then i will have the following exception thrown:

The ViewData item that has the key 'ChosenDay' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.

This errors gets thrown at the line in my view where i display the dropdown list.

I really have no idea how to solve this and tried several solutions i found online. but none of them really worked.

Ty in advance!

  • 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-14T15:05:04+00:00Added an answer on May 14, 2026 at 3:05 pm

    I have seen such an error.
    This is becouse ViewData["days"] as SelectList is null when the view is rendered. This can be becouse of ViewData[“days”] is null or in different type then SelectList.
    The problem must be found here:

    [HttpPost]
    public ActionResult Registreer(EventRegistreerViewModel model)
    {
    }
    

    Make shure, that this code

    SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
     ViewData["days"] = _list;
    

    runs and that ViewData[“days”] not null and IEnumerable before you return View. It must be becouse of Model.IsValid so ViewData[“days”] not binded.

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

Sidebar

Related Questions

No related questions found

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.