传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [Demo.Models.CustomerData]',但是此字典需要类型为'Demo.Models.CustomerData'的模型项. [英] The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Demo.Models.CustomerData]', but this dictionary requires a model item of type 'Demo.Models.CustomerData'.

查看:168
本文介绍了传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [Demo.Models.CustomerData]',但是此字典需要类型为'Demo.Models.CustomerData'的模型项.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决这种类型的问题?本身!

现在,很明显该错误表明存在数据类型不匹配.您传递的模型项不是预期的Demo.Models.CustomerData类型.相反,您传递的是Demo.Models.CustomerData列表.

解决数据类型问题.


嗨Sajan,

我今天也面临着类似的问题.例外与您一样,但出于不同的原因.在单个视图中,我想放置三个不同的控件.它们都是强类型A的类型.首先是编辑器控件,其次是只读的,看起来与表单相似,第三个是记录集中的代表项.这些是我创建的文件:

Controllers/ActionController.cs
型号/A.cs
型号/ActionViewModel.cs
视图/共享/DisplayTemplates/A.cshtml-类似于display
的表单 视图/共享/DisplayTemplates/AItem.cshtml-表显示项
视图/共享/EditorTemplates/A.cshtml-可编辑表格
Views/Action/Action.cshtml-页面的主视图

Views/Action/Action.cshtml清单

 @ model ModelItemType.Models.ActionViewModel

@ {
    ViewBag.Title =动作";
}

<   h2  > 动作<  /h2  > 
@if(Model.AInstance.IsEditable)
{
@ Html.EditorFor(model => model.AInstance)
}
别的
{
@ Html.DisplayFor(model => model.AInstance)
}
@if(Model.ACollection.Count> 0)
{
<   > 
@ Html.DisplayFor(model => model.ACollection,"AItem")
<  /table  > 
} 



视图/共享/EditorTemplates/A.cshtml列表

 @ model ModelItemType.Models.A
@using(Html.BeginForm())
{
    @ Html.ValidationSummary(true)
    <  字段集 > 
        @ Html.EditorFor(模型=>模型.MyProperty)
        @ Html.ValidationMessageFor(model => model.MyProperty)
        <   p  > 
            <  输入    ="  提交"   保存"   / > 
        <  /p  > 
    <  /fieldset  > 
} 



视图/共享/DisplayTemplates/A.cshtml列表

 @ model ModelItemType.Models.A

@ Html.DisplayFor(model => model.MyProperty)



视图/共享/DisplayTemplates/AItem.cshtml清单

 @ model ModelItemType.Models.A

<   tr  > 
    <   td  >  @Html. DisplayFor(model => model.MyProperty)<  /td  <  /tr  >  



Controllers/ActionController.cs列表

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 ModelItemType.Models;

命名空间 ModelItemType.Controllers
{
    公共  ActionController:控制器
    {
        //  
        //  GET:/Action/

        公共 ActionResult Action()
        {
            ActionViewModel viewModel = 
            {
                AInstance =  A {MyProperty = "  span>,IsEditable =  true },
                ACollection = 列表< a> {
                     A {MyProperty = "  ,IsEditable =  true },
                     A {MyProperty = "  ,IsEditable =  false }
                }
            };
            返回 View(viewModel);
        }

    }
} 



型号/A.cs列表

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;

命名空间 ModelItemType.Models
{
    公共  A
    {
        公共 字符串 MyProperty { get ; 设置; }
        公共 布尔 IsEditable { get ; 设置; }
    }
} 



Models/ActionViewModel.cs列表

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;

命名空间 ModelItemType.Models
{
    公共  ActionViewModel
    {
        公共一个实例{ get ; 设置; }
        公共 ICollection< a> ACollection {获取; 设置; }
    }
} 



此代码引发与该帖子标题类似的异常.查看Views/Action/Action.cshtml:

 @ Html.DisplayFor(model => model.ACollection,"AItem")


尽管如果将行更改为:

 @ Html.DisplayFor(model => model.ACollection)


模型类是相同的.但是我无法绑定两个控件:一个绑定到单个实例,一个绑定到集合.

您修改后的问题是:
不能显示数据库中的数据列表,并像第一个视图一样将表单填写到同一页面"
解决方法是:
我们可以!只需对视图执行以下操作:
1.在Views/Action/Action.cshtml中更改以下行:

 @ Html.DisplayFor(model => model.ACollection,"AItem")


至:

 @ Html.DisplayFor(model => model.ACollection)


2.删除视图/共享/DisplayTemplates/A.cshtml
3.将Views/Shared/DisplayTemplates/AItem.cshtml重命名为Views/Shared/DisplayTemplates/A.cshtml

我认为这可以解决您的问题.

不幸的是,我和我在一起.为什么我的第一个版本不想工作?

亲切的问候,
Paweł


how to solve this type of problem?

解决方案

First of all, the title is for short title related to issue asked and not for full error or the question itself!

Now, clearly the error specifies that there is a datatype mis-match. The model item that you pass is not of type Demo.Models.CustomerData as expected. Instead you are passing a list of Demo.Models.CustomerData.

Resolve the datatype issue.


Hi Sajan,

I have faced today a similar problem. The Exception was like yours but for a diffrent reasons. In a single View I wanted to place three different controls. All of them strongly typed to class A. First was editor control, second was displaying read only and looked similar to a form and third represented item in a collection of records. These are the files I have created:

Controllers/ActionController.cs
Models/A.cs
Models/ActionViewModel.cs
Views/Shared/DisplayTemplates/A.cshtml - form like display
Views/Shared/DisplayTemplates/AItem.cshtml - item for a table display
Views/Shared/EditorTemplates/A.cshtml - ediatble form
Views/Action/Action.cshtml - main view for the page

Views/Action/Action.cshtml Listing

@model ModelItemType.Models.ActionViewModel

@{
    ViewBag.Title = "Action";
}

<h2>Action</h2>
@if (Model.AInstance.IsEditable)
{
@Html.EditorFor(model => model.AInstance)
}
else
{
@Html.DisplayFor(model => model.AInstance)
}
@if (Model.ACollection.Count > 0)
{
<table>
@Html.DisplayFor(model => model.ACollection, "AItem")
</table>
}



Views/Shared/EditorTemplates/A.cshtml Listing

@model ModelItemType.Models.A
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        @Html.EditorFor(model => model.MyProperty)
        @Html.ValidationMessageFor(model => model.MyProperty)
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}



Views/Shared/DisplayTemplates/A.cshtml Listing

@model ModelItemType.Models.A

@Html.DisplayFor(model => model.MyProperty)



Views/Shared/DisplayTemplates/AItem.cshtml Listing

@model ModelItemType.Models.A

<tr>
    <td>@Html.DisplayFor(model => model.MyProperty)</td>
</tr>



Controllers/ActionController.cs Listing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ModelItemType.Models;

namespace ModelItemType.Controllers
{
    public class ActionController : Controller
    {
        //
        // GET: /Action/

        public ActionResult Action()
        {
            ActionViewModel viewModel = new ActionViewModel
            {
                AInstance = new A { MyProperty = "PropertySet1", IsEditable = true },
                ACollection = new List<a> {
                    new A{ MyProperty = "PropertySet2", IsEditable = true},
                    new A{ MyProperty = "PropertySet3", IsEditable = false}
                }
            };
            return View(viewModel);
        }

    }
}



Models/A.cs Listing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ModelItemType.Models
{
    public class A
    {
        public string MyProperty { get; set; }
        public bool IsEditable { get; set; }
    }
}



Models/ActionViewModel.cs Listing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ModelItemType.Models
{
    public class ActionViewModel
    {
        public A AInstance { get; set; }
        public ICollection<a> ACollection { get; set; }
    }
}



This code throws the similar exception as in the title of this post. Look at the Views/Action/Action.cshtml:

@Html.DisplayFor(model => model.ACollection, "AItem")


Although if we change the line to:

@Html.DisplayFor(model => model.ACollection)


The model class is the same. But I was not able to have two controls bound: one to the single instance one to the collection.

Your edited question was:
"cant we show the list of data from database and fill up form to the same page like 1st view"
Solution is:
Yes we can! Just do the following to the views:
1. Change the following line in Views/Action/Action.cshtml:

@Html.DisplayFor(model => model.ACollection, "AItem")


to:

@Html.DisplayFor(model => model.ACollection)


2. Remove Views/Shared/DisplayTemplates/A.cshtml
3. Rename Views/Shared/DisplayTemplates/AItem.cshtml to Views/Shared/DisplayTemplates/A.cshtml

I think this solves your problem.

Unfortunately I''m left with mine. Why my first version does not want to work?

Kind Regards,
Paweł


这篇关于传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [Demo.Models.CustomerData]',但是此字典需要类型为'Demo.Models.CustomerData'的模型项.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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