强类型的视图通过对ViewData的一个DropDownList中的SelectList:类型不匹配上提交 [英] Strongly typed view with a SelectList for DropDownList via ViewData: type mismatch on submit

查看:189
本文介绍了强类型的视图通过对ViewData的一个DropDownList中的SelectList:类型不匹配上提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造ASP.NET MVC2 RC 2的形式,是基于日历事件对象。该对象具有eventTypeId这是我需要通过选择列表来填充System.Int32。

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>

这导致的HTML:

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)
{
    ...

(我已经尝试添加 [绑定(不包括=eventTypeId)] 在CalendarEvent newEvent参数的前面,但它不会改变的行为。)

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

问题:当我提交表单,我得到一个InvalidOperationException异常:

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

这有钥匙的ViewData的项目
  eventTypeId是类型
  System.Int32,但必须是类型
  IEnumerable的&LT; SelectListItem&GT;

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

我已经看了一些例子在这里和在MVC博客,但到目前为止还不清楚这是如何工作的(它看起来像基于许多例子,它应该工作原样)。我是否需要创建一个具有类型SelectListItem的变量接受SelectListItem和值转换为实际设置eventTypeId一个System.Int32一个第二个模型?这似乎相当的四围。

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.

推荐答案

想了解更多这方面的一些之后,我想,也许我需要填充计算机[eventTypeID] 在接收公布值控制器动作 - 不只是在设置形式控制器动作。我想,和它的工作。

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.

这是接受POST被更改的控制器操作(添加在此列表,最后两行):

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");
        ....

这是我不太清楚,所以希望别人认为这有用的。我检查与插件的LiveHTTPHeaders为Firefox的实际HTTP POST确实entryTypeID发布的...&安培; entryTypeId = 2及...(我已经选择了第二项表单上提交前),但做我们刷新选择名单张贴到控制器做验证?

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?

这篇关于强类型的视图通过对ViewData的一个DropDownList中的SelectList:类型不匹配上提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆