2012-02-09 49 views
0

我嘗試了簡單的ASP.NET MVC2下拉列表,但從tryupdatemodel下面得到錯誤。如何修復MVC2下拉列表中的轉換錯誤

如何解決這個問題,everthing看起來正確?

觀點:

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

型號:

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

public string DeliveryPlace { get; set; } 

結果:

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 

回答

1

的問題是,你的HTML輸入被命名爲Post24DeliveryPlaces和它試圖一個字符串綁定對此。

POST數據是這樣看:

POST 
{ 
    Post24DeliveryPlaces = (string) 
} 

然而,在你的模型,Post24DeliveryPlaces是一個IEnumerable,它不能在字符串轉換爲SelectListItem。

Html.DropDownList將字段名稱作爲第一個參數。嘗試更改爲此,將交貨地點列表作爲第二個參數。

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

這將生成的HTML

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