Strongly typed view with a SelectList for DropDownList via ViewData: type mismatch on submit?

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?

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?

6 Well think about it this way. The DropDownList submits a single value to the server. The value that the user selects.It doesn't submit all the possible values that are in the drop down list.

That's just not the way HTML works, which perhaps is kind of unfortunate. That's why you need to repopulate the viewdata in the POST request to indicate what the range of possible values are. You don't need to repopulate the selected value.

– Haacked Apr 9 '10 at 22:17 1 Thank you so much for this solution, is definitely not intuitive, but it does make sense when you think about it :) – jamiebarrow Aug 12 '10 at 11:20 I got caught by another unintuitive thing today that made sense: say you have a Html. RenderAction("LoginBox") and the LoginBox action has both a GET and POST. If you submit a form for something else unrelated to LoginBox (but it happens to be on the same page via RenderAction) and then return the view on validation failure, the POST version of LoginBox is called/returned not the GET.

This all makes sense but there is some tricky angles! Edit: Whoops, replied to completely wrong thread here. – Cymen Feb 9 at 5:20.

This problem occurs if the ViewData does not contain required values for all the fields on the view and the ViewData is posted back to the view.

The object has eventTypeId which is a System. Int32 that I need to populate with via a select list. 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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions