ASP.NET MVC 2 - Html.DropDownList与ViewModel的混淆

Sun*_*oot 32 asp.net-mvc html-helper viewmodel asp.net-mvc-2 drop-down-menu

我对如何在ASP.NET MVC 2.0 R2上使用新的强类型Html.DropDownListFor帮助程序感到十分迷茫和困惑

在我写的视图中:

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

<%= Html.ValidationMessageFor(m => m.ParentCategory)%>
Run Code Online (Sandbox Code Playgroud)

因此我的Model对象是:

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

<%= Html.DropDownListFor(m => m.ParentCategory.**CategoryId**, 
                              new SelectList .../ snip  ) %>
Run Code Online (Sandbox Code Playgroud)

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

Ram*_* A. 29

我也遇到了同样的问题.

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

{"的参数转换由类型 'System.String' 为类型"System.Collections.Generic.KeyValuePair`2 [[System.String,mscorlib程序,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089],[系统. Int64的,mscorlib程序,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089]]"失败,因为没有类型转换器可以将这些类型之间的转换."} {System.Exception的} System.InvalidOperationException

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

<%=Html.DropDownListFor(x => x.MyDictionary, 
                             new SelectList(
                                 Model.MyDictionary, 
                                 "Value", 
                                 "Key")) %>
Run Code Online (Sandbox Code Playgroud)

当我把它改为:

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

它没有问题.

谢谢.


小智 9

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

您是否尝试将Model.ParentCategory.CategoryId最后一个参数用作SelectList 并删除[None]参数?


Per*_*eck 8

我会摆脱

public Category ParentCategory { get; set; }
Run Code Online (Sandbox Code Playgroud)

并做一个

public int? CategoryId { get; set; }
Run Code Online (Sandbox Code Playgroud)

代替.您可能只需要Id - 您可以始终使用Id作为键查找实际对象(在您的情况下使用列表中的linq/lambda).

该视图将具有以下两个:

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

<%= Html.ValidationMessageFor(m => m.CategoryId)%>
Run Code Online (Sandbox Code Playgroud)