gpt4 book ai didi

c# - 系统.InvalidOperationException : The model item passed into the dictionary is of type

转载 作者:太空狗 更新时间:2023-10-30 01:36:34 24 4
gpt4 key购买 nike

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

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 并使用以下代码添加了一个 View :

@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>

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

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'WebApplication4.Models.Class1+Customer', but this dictionary requires a model item of type 'WebApplication4.Models.Class1'.


ok,我把Class1.cs下的neSTLed类拿出来了,

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

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

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

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

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

最佳答案

您的 Controller 正在实例化 Class1.Customer() 类型的对象传递给 Razor View ,而您的 View 已被告知需要类型为 Class1 的模型.

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

如果是,将 View 更改为:

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

Controller (/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...不是<%= Model....%>像网络表单。

@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>

关于c# - 系统.InvalidOperationException : The model item passed into the dictionary is of type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21737203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com