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

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

问题描述





传入字典的模型项目类型为'System.Collections.Generic.List`1 [MvcApplication5.Models.Employee ]',但这个字典需要一个'MvcApplication5.Models.Test1'类型的模型项。




我收到此错误。以下是我的代码。请告诉我我哪里错了。





控制器类



Hi,

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication5.Models.Employee]', but this dictionary requires a model item of type 'MvcApplication5.Models.Test1'.


I'm getting this error. Below is my code. Please tell me where i'm wrong.


Controller Class

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

namespace MvcApplication5.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        MvcApplication5.Models.TestEntities te = new MvcApplication5.Models.TestEntities();
        public ActionResult Index()
        {
            List<Employee> emp = te.Employees.ToList();

            return View(emp);
        }

    }
}





模特类





Model Class

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

namespace MvcApplication5.Models
{
    public class Test1
    {
        public int Code { get; set; }
        public string ID { get; set; }
        public string Name { get; set; }
        public string Department { get; set; }
    }

}







查看




View

@model MvcApplication5.Models.Test1

@{
    ViewBag.Title = "Index";
}

<html>
<body>
<div>
@Html.LabelFor(Model=>Model.ID.ToString())
@Html.LabelFor(Model=>Model.Code.ToString())
@Html.LabelFor(Model=>Model.Name.ToString())
@Html.LabelFor(Model=>Model.Department.ToString())

</div>
</body>
</html>

推荐答案

将视图返回模型更改为Employee,如下所示。例外是因为您返回Type Employee的集合,但视图期望类型为Test1。如果要将集合返回到视图,则必须将视图返回类型更改为IEnumerable< type>如下所示

Change the views return model to Employee like below.The Exception is because you are returning the collection of Type Employee but the views expect type Test1. If you want to return a collection to a view you must change the view return type to IEnumerable<type> like below
@model IEnumerable<yournamespace.employee>

@{
    ViewBag.Title = "Index";
}
<html>
<body>
<div>
@foreach(var item in Model){
@Html.LabelFor(item.ID.ToString())
@Html.LabelFor(item.Code.ToString())
@Html.LabelFor(item.Name.ToString())
@Html.LabelFor(item.Department.ToString())
 }
</div>
</body>
</html>
</yournamespace.employee>





如果你想要将特定记录返回到视图使用如下所示



If you want to return a particular record in to the view use like below

@model YourNameSpace.Employee
 
@{
    ViewBag.Title = "Index";
}
 
<html>
<body>
<div>
@Html.LabelFor(Model=>Model.ID.ToString())
@Html.LabelFor(Model=>Model.Code.ToString())
@Html.LabelFor(Model=>Model.Name.ToString())
@Html.LabelFor(Model=>Model.Department.ToString())
 
</div>
</body>
</html>





并且您还必须将员工类型返回到ActionResult而不是List< employee>如下所示



and also you must return Employee type in to that ActionResult not List<employee> like below

public ActionResult Index()
       {
         Employee emp= te.Employees.FirstOrDefault(x=>x.id=1)

           return View(emp);
       }


这是siruxgh
This is siruxgh
<pre lang="c#"><pre lang="c#"><pre lang="c#">


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

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