I am trying to create a form in ASP.NET MVC2 RC 2 that is based on a calendar event object. The object has an eventTypeId, which is System.Int32, which I need to populate with a select list.
Controller to create the initial view:
[WAuthorize]
public ActionResult AddCalendarEvent()
{
CalendarEventTypesManager calendarEventTypesManager =
new CalendarEventTypesManager();
ViewData["eventTypeId"] = new SelectList(
calendarEventTypesManager.SelectAll(), "Id", "Type");
return View();
}
View snippet (with title):
<%@ Page Title="" Language="C#"
MasterPageFile="~/Views/Shared/Site.Extranet.master"
Inherits="System.Web.Mvc.ViewPage<SomeProject.Models.CalendarEvent>" %>
...
<p><%= Html.DropDownList("eventTypeId") %></p>
The result is HTML code:
<p>
<select id="eventTypeId" name="eventTypeId">
<option value="1">All school activities</option>
<option value="2">All school event</option>
</select>
</p>
POST Receive Controller:
[WAuthorize]
[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult AddCalendarEvent(CalendarEvent newEvent)
{
...
(I tried adding [Bind (Exclude="eventTypeId")]the "CalendarEvent newEvent" before the parameter, but it did not change the behavior.)
Problem: when I submit the form, I get an InvalidOperationException:
The ViewData element with the key 'eventTypeId' is of type 'System.Int32', but must be of type 'IEnumerable <SelectListItem>'.
MVC, , (, , ). , SelectListItem, SelectListItem System.Int32, eventTypeId? .