ASP.NET MVC 2 - Html.DropDownListFor与视图模型混乱 [英] ASP.NET MVC 2 - Html.DropDownListFor confusion with ViewModel

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

问题描述

我得到完全失去了和困惑就如何使用新的强类型Html.DropDownListFor帮手在ASP.NET MVC 2.0 R2

在查看我书面方式:

 <%= Html.DropDownListFor(M = GT; m.ParentCategory,新的SelectList(Model.Categories,类别ID,姓名,Model.ParentCategory),[无])%>&所述;%= Html.ValidationMessageFor(M => m.ParentCategory)%GT;

和我的模型对象是这样的:

 公共类CategoryForm:FormModelBase
{
    公共CategoryForm()
    {
        分类=新的List<类别及GT;();        Categories.Add(新CategoryForm.Category(){
                           类别ID = 1,
                           NAME =处理器});
        Categories.Add(新CategoryForm.Category(){
                           类别ID = 2,
                           NAME =内存});
        Categories.Add(新CategoryForm.Category(){
                           类别ID = 3,
                           NAME =硬盘驱动器});
    }    // ...等道具,略... //    公共范畴ParentCategory {搞定;组; }    公众的IList<类别及GT;分类{搞定;保护套; }    公共类目录
    {
        公众诠释? {类​​别ID获取;组; }
        公共字符串名称{;组; }
    }
}

问题是,当我从下拉列表中的项目,说的第一个项目,我得到以下ValidationMessageFor错误的值1是无效的。

于是我改变视图...

 <%= Html.DropDownListFor(M =方式> m.ParentCategory ** **的CategoryId,
                              新的SelectList ... / SNIP)%GT;

现在它的工作原理,有点。在我的ViewModel的ParentCategory属性设置了正确的类别ID',但'名称'为NULL。我是不是最好只具有ParentCategory财产,而不是一个强类型的类别的对象一个可空INT?


解决方案

我也遇到同样的问题。

当我调试的行动,并期待在ModelState.Values​​ [1] .Errors [0] .Exception例如,我看到以下内容:

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

在我的方案,是从字典中创建我的SelectList和我用这个在我看来:

 <%= Html.DropDownListFor(X => x.MyDictionary,
                             新的SelectList(
                                 Model.MyDictionary,
                                 值,
                                 键))%GT;

当我把它改为:

 <%= Html.DropDownListFor(X => x.MyDictionary.Keys,//<  - 改变。键
                             新的SelectList(
                                 Model.MyDictionary,
                                 值,
                                 键))%GT;

这工作没有问题。

感谢您。

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; }
    }
}

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  ) %>

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.

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

{"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}

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")) %>

When I changed it to:

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

It worked without issues.

Thank you.

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

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