如何解决在MVC2的DropDownList转换错误 [英] How to fix conversion error in MVC2 dropdownlist

查看:178
本文介绍了如何解决在MVC2的DropDownList转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简单的DropDownList在ASP.NET MVC2但tryupdatemodel得到下面的错误。

如何解决这个问题,全部的寄托看起来是正确的?

查看:

 <%= Html.DropDownList(Post24DeliveryPlaces,Model.DeliveryPlace)%GT;

模型:

 公开的IEnumerable< SelectListItem> Post24DeliveryPlaces {搞定;组; }公共字符串DeliveryPlace {搞定;组; }

结果:

  System.InvalidOperationException:从类型'System.String'的参数转换为键入'System.Web.Mvc.SelectListItem'失败,因为没有类型转换器可以将这些类型之间的转换。在System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo的文化,对象的值,类型destinationType) 在System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo的文化,对象的值,类型destinationType)在System.Web.Mvc.ValueProviderResult.ConvertTo(类型类型,CultureInfo的文化)在System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary的ModelState,字符串modelStateKey,ValueProviderResult valueProviderResult,类型destinationType


解决方案

的问题是,你的HTML的输入被命名为Post24DeliveryPlaces和它试图一个字符串结合。

POST数据看起来像这样:

  POST
{
    Post24DeliveryPlaces =(字符串)
}

不过,在你的模型,Post24DeliveryPlaces是一个IEnumerable,它不能在字符串转换为SelectListItem。

Html.DropDownList 需要的字段名称作为第一个参数。尝试改变这一点,给人的交货地点名单,作为第二个参数。

  Html.DropDownList(DeliveryPlace,Model.Post24DeliveryPlaces)

这将生成的HTML

 <选择名称=DeliveryPlace>
    <期权价值=...> ....< /选项>
    ....
< /选择>

I tried simple dropdownlist in ASP.NET MVC2 but got error below from tryupdatemodel.

How to fix this, everthing looks correct ?

view:

   <%= Html.DropDownList("Post24DeliveryPlaces", Model.DeliveryPlace)%>

model:

public IEnumerable<SelectListItem> Post24DeliveryPlaces { get; set; }

public string DeliveryPlace { get; set; }

result:

System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types.  

at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)  

 at System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType)  

at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture)   at System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType

解决方案

The problem is that your html input is being named Post24DeliveryPlaces and it's trying to bind a single string to that.

The Post data is looking like this:

POST
{
    Post24DeliveryPlaces = (string)
}

However, in your model, Post24DeliveryPlaces is an IEnumerable and it can't convert at string to SelectListItem.

Html.DropDownList takes the field name as the first parameter. Try changing to this, giving the list of delivery places as the second parameter.

Html.DropDownList("DeliveryPlace", Model.Post24DeliveryPlaces)

This will generate the Html

<select name="DeliveryPlace">
    <option value="..">....</option>
    ....
</select>

这篇关于如何解决在MVC2的DropDownList转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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