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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:33:41+00:00 2026-05-13T20:33:41+00:00

I am trying to create a form in ASP.NET MVC2 RC 2 that is

  • 0

I am trying to create a form in ASP.NET MVC2 RC 2 that is based on a calendar event object. The object has eventTypeId which is a System.Int32 that I need to populate with via a select list.

The controller to create the initial view is:

[WAuthorize]
public ActionResult AddCalendarEvent()
{
    CalendarEventTypesManager calendarEventTypesManager = 
        new CalendarEventTypesManager();

    ViewData["eventTypeId"] = new SelectList(
        calendarEventTypesManager.SelectAll(), "Id", "Type");

    return View();
}

The snippet of the View (with the header) is:

<%@ Page Title="" Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Extranet.master"
    Inherits="System.Web.Mvc.ViewPage<SomeProject.Models.CalendarEvent>" %>

...

<p><%= Html.DropDownList("eventTypeId") %></p>

Which results the HTML of:

<p>
<select id="eventTypeId" name="eventTypeId">
    <option value="1">All school activities</option> 
    <option value="2">All school event</option> 
</select>
</p> 

The POST-accepting controller is:

[WAuthorize]
// TODO research some more
[ValidateInput(false)]              
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult AddCalendarEvent(CalendarEvent newEvent)
{
    ...

(I’ve tried adding [Bind (Exclude="eventTypeId")] in front of the “CalendarEvent newEvent” parameter but it does not change the behavior.)

Problem: When I submit the form, I get an InvalidOperationException exception:

The ViewData item that has the key
‘eventTypeId’ is of type
‘System.Int32’ but must be of type
‘IEnumerable<SelectListItem>’.

I’ve looked at a number of examples here and on the MVC blogs but so far it isn’t clear how this is supposed to work (it looks like based on many examples, it should work as is). Do I need to create a second model that has a variable of type SelectListItem to accept the SelectListItem and convert the value to a System.Int32 to actually set eventTypeId? That seems rather round about.

  • 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-13T20:33:41+00:00Added an answer on May 13, 2026 at 8:33 pm

    After thinking about this some more, I thought that maybe I needed to populate ViewData["eventTypeID"] in the controller action that receives the posted values — not just in the controller action that sets up the form. I tried that and it worked.

    The controller action that accepts the POST was altered (adding the last two lines in this listing):

        [WAuthorize]
        [ValidateInput(false)]              // TODO research some more
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult AddCalendarEvent(CalendarEvent newEvent)
        {
            CalendarEventTypesManager calendarEventTypesManager = new CalendarEventTypesManager();
            ViewData["eventTypeId"] = new SelectList(calendarEventTypesManager.SelectAll(), "Id", "Type");
            ....
    

    That was not clear to me so hopefully someone else finds this useful too. I checked the actual HTTP POST with LiveHTTPHeaders plugin for Firefox and indeed entryTypeID is posted as “…&entryTypeId=2&…” (I had selected the second item on the form before submitting) but do we reload the select list in the posted-to controller to do validation?

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

Sidebar

Related Questions

I am trying to create an asp.net web form that allows a user to
I am trying to create a custom authentication scheme in ASP.NET MVC using form
I'm trying to create a form that has an expandable widget. The problem is
I'm trying to create a form that based on the users input would determine
I am new to ASP.NET, and I am trying to create a contact form
I'm trying to create a job in an ASP.NET (C#) form using Quartz.NET, and
I’m just learning ASP.NET MVC and I’m trying to create a mock form request
I am trying to debug an asp.net web form that requires a value from
I am trying to create a login form on my ASP.NET website. Currently, there's
I'm trying to create a composite ASP.NET control that let's you build an editable

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.