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

牧云@^-^@ 提交于 2019-12-03 15:44:54

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?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!