C# System.InvalidOperationException:传递到字典的模型项的类型为
我正在编写一个代码,将创建一个对象customer并显示它 Class1.cs是我正在使用的模型:C# System.InvalidOperationException:传递到字典的模型项的类型为,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我正在编写一个代码,将创建一个对象customer并显示它 Class1.cs是我正在使用的模型: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplicatio
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>
@model WebApplication4.Models.Class1
@{
ViewBag.Title=“Index”;
}
文件标题
客户id为:
客户代码为:
客户金额为:
当我运行代码时,我得到:
异常详细信息:System.InvalidOperationException:传入字典的模型项类型为“WebApplication4.Models.Class1+Customer”,但此字典需要类型为“WebApplication4.Models.Class1”的模型项
好的,我在Class1.cs下创建了nestled类 所以现在它只说公共类客户 我还更改了名称空间WebApplication4.Models 在DefaultController1.cs下命名WebApplication4.Controller 但是当我在Index.cshtml下时,它说
命名空间WebApplication4中不存在类型或命名空间Class1。模型您的控制器正在实例化传递给razor视图的
Class1.Customer()
类型的对象,而您的视图被告知需要Class1
类型的模型
您有一个嵌套类-我不确定这是否是故意的
如果是,请将视图更改为:
@model WebApplication4.Models.Class1.Customer
编辑我编译并运行了以下内容: 型号(/Models/Customer.cs) 控制器(/Controllers/Default1Controller.cs) 查看(
/Views/Default1/Index.cshtml
)。注意razor使用
@Model…
而不是
像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>
@user2439070我已经编译并运行了上面的代码。请注意razor的
@
语法与ok我已经做了这些更改,但它仍然无法在视图下的@model WebApplication4.Models.Customer行中将客户识别为模型
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);
}
}
}
@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>
<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>