【问题标题】:System.InvalidOperationException: The model item passed into the dictionary is of typeSystem.InvalidOperationException:传入字典的模型项是类型
【发布时间】:2014-02-12 19:01:38
【问题描述】:

我正在编写一个代码来创建一个对象客户并显示它

Class1.cs 是我正在使用的模型:

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

namespace WebApplication4.Models                        
{                       
    public class Class1                     
    {                       
        public class Customer                       
        {                       
            public int Id { set; get; }                     
            public string CustomerCode { set; get; }                        
            public double Amount { set; get; }                      


        }                       

    }                       
}                       

在 Default1Controller 我有:

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


using WebApplication4.Models ;          


namespace WebApplication4.Models            
{           
    public class Default1Controller : Controller            
    {           
        //          
        // GET: /Default1/          
        public ActionResult Index()         

        {           
            Class1.Customer ob = new Class1.Customer();         

            ob.Id = 1001;           
            ob.CustomerCode = "C001";           
            ob.Amount = 900.23;         
            return View(ob);            
        }           
    }       
}       

我右键单击 ActionResult 并添加了一个包含以下代码的视图:

@model WebApplication4.Models.Class1

@{
    ViewBag.Title = "Index";
}

    <html>


    <head>
        <title>Title of the document</title>
    </head>

        <body>

            <div> 
                 The customer id is: <% = | Model.Id %>  < br/>
                 The customer Code  is: <% = | Model.CustomerCode  %>  < br/>
                 The customer Amount is: <% = | Model.Id %>  < br/>

            </div>
</body>

</html>

当我运行代码时,我得到:

异常详细信息:System.InvalidOperationException:传入字典的模型项的类型为“WebApplication4.Models.Class1+Customer”,但此字典需要类型为“WebApplication4.Models.Class1”的模型项。


好的,我把Class1.cs下的嵌套类拿出来了,

所以现在它只说公共类客户。

我还更改了命名空间 WebApplication4.Models

到 DefaultController1.cs 下的命名空间 WebApplication4.Controller,

但是当我进入 Index.cshtml 时,它会说

命名空间 WebApplication4.Models 中不存在类型或命名空间 Class1

【问题讨论】:

    标签: c# asp.net-mvc model-view-controller


    【解决方案1】:

    您的控制器正在实例化一个类型为 Class1.Customer() 的对象,并传递给 razor 视图,而您的视图被告知需要一个类型为 Class1 的模型。

    你有一个嵌套类 - 我不确定这是不是故意的?

    如果是,将视图更改为:

    @model WebApplication4.Models.Class1.Customer
    

    编辑
    我已经编译并运行了这个:

    模型 (/Models/Customer.cs)

    namespace WebApplication4.Models
    {
        public class Customer
        {
            public int Id { set; get; }
            public string CustomerCode { set; get; }
            public double Amount { set; get; }
        }
    }
    

    控制器 (/Controllers/Default1Controller.cs)

    using System.Web.Mvc;
    using WebApplication4.Models;
    
    namespace WebApplication4.Controllers
    {
        public class Default1Controller : Controller
        {
            public ActionResult Index()
            {
                var ob = new Customer
                {
                    Id = 1001, 
                    CustomerCode = "C001", 
                    Amount = 900.23
                };
    
                return View(ob);
            }
        }
    }
    

    查看 (/Views/Default1/Index.cshtml)。
    注意 razor 使用 @Model... 而不是 &lt;%= Model....%&gt; 像 web 表单。

    @model WebApplication4.Models.Customer
    
    @{
        ViewBag.Title = "Index";
    }
    
    <html>
    <head>
        <title>Title of the document</title>
    </head>
    <body>
        <div>
            The customer id is: @Model.Id  < br/>
            The customer Code  is: @Model.CustomerCode  < br/>
            The customer Amount is: @Model.Amount < br/>
        </div>
    </body>
    </html>
    

    MyApp/Default1/Index生成以下页面

    <html>
    <head>
        <title>Title of the document</title>
    </head>
    <body>
        <div>
            The customer id is: 1001  < br/>
            The customer Code  is: C001  < br/>
            The customer Amount is: 900.23 < br/>
        </div>
    </body>
    </html>
    

    【讨论】:

    • @user2439070 我已经编译并运行了上面的。请注意 razor 的 @ 语法与 webforms 的 &lt;%= 语法。
    • 好的,我已经进行了这些更改,但它仍然没有将 Customer 识别为视图下 @model WebApplication4.Models.Customer 行中的模型
    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2015-09-19
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2012-06-11
    相关资源
    最近更新 更多