ASP.NET MVC 2 - Html.DropDownList 与 ViewModel 混淆 [英] ASP.NET MVC 2 - Html.DropDownListFor confusion with ViewModel

查看:20
本文介绍了ASP.NET MVC 2 - Html.DropDownList 与 ViewModel 混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何在 ASP.NET MVC 2.0 R2 上使用新的强类型 Html.DropDownListFor 助手完全迷失和困惑

I'm getting totally lost and confused on how to use the new strongly typed Html.DropDownListFor helper on ASP.NET MVC 2.0 R2

在我正在编写的视图中:

In the View I'm writting:

<%= Html.DropDownListFor(m => m.ParentCategory, new SelectList(Model.Categories, "CategoryId", "Name", Model.ParentCategory), "[ None ]")%>

<%= Html.ValidationMessageFor(m => m.ParentCategory)%>

我的模型对象是这样的:

and my Model object is thus:

public class CategoryForm : FormModelBase
{
    public CategoryForm()
    {
        Categories = new List<Category>();

        Categories.Add(new CategoryForm.Category() {
                           CategoryId = 1, 
                           Name = "CPUs" });
        Categories.Add(new CategoryForm.Category() { 
                           CategoryId = 2, 
                           Name = "Memory" });
        Categories.Add(new CategoryForm.Category() { 
                           CategoryId = 3, 
                           Name = "Hard drives" });
    }

    // ...other props, snip... //

    public Category ParentCategory { get; set; }

    public IList<Category> Categories { get; protected set; }

    public class Category
    {
        public int? CategoryId { get; set; }
        public string Name { get; set; }
    }
}

问题是,当我从下拉列表中选择一个项目时,比如说第一个项目,我收到以下 ValidationMessageFor 错误值 '1' 无效."

The problem is that when I select an item from the dropdown list, say the first item, I get the following ValidationMessageFor error "The value '1' is invalid."

所以我将视图更改为...

So I change the View to...

<%= Html.DropDownListFor(m => m.ParentCategory.**CategoryId**, 
                              new SelectList .../ snip  ) %>

现在可以了,有点.我的 ViewModel 中的 ParentCategory 属性设置为正确的CategoryId",但Name"为 NULL.我是否最好只为 ParentCategory 属性设置一个可为空的 int 而不是强类型的Category"对象?

Now it works, kinda. The ParentCategory property in my ViewModel is set with the correct 'CategoryId' but the 'Name' is NULL. Am I better off just having a nullable int for ParentCategory property instead of a strongly typed 'Category' object?

推荐答案

我也遇到了同样的问题.

I was also experiencing the same issue.

例如,当我调试 Action 并查看 ModelState.Values[1].Errors[0].Exception 时,我看到以下内容:

When I debug the Action and look at the ModelState.Values[1].Errors[0].Exception for example, I see the following:

{"从类型'System.String'到类型'System.Collections.Generic.KeyValuePair`2的参数转换[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' 失败,因为没有类型转换器可以在这些类型之间进行转换."} System.Exception {System.InvalidOperationException}
{"The parameter conversion from type 'System.String' to type 'System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' failed because no type converter can convert between these types."} System.Exception {System.InvalidOperationException}

在我的场景中,我的 SelectList 是从字典创建的,我在我的视图中使用它:

In my scenario, my SelectList is created from a Dictionary and i use this in my view:

<%=Html.DropDownListFor(x => x.MyDictionary, 
                             new SelectList(
                                 Model.MyDictionary, 
                                 "Value", 
                                 "Key")) %>

当我把它改成:

<%=Html.DropDownListFor(x => x.MyDictionary.Keys, // <-- changed to .Keys
                             new SelectList(
                                 Model.MyDictionary, 
                                 "Value", 
                                 "Key")) %>

它没有问题.

谢谢.

这篇关于ASP.NET MVC 2 - Html.DropDownList 与 ViewModel 混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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