use*_*070 5 c# model-view-controller asp.net-mvc
我正在编写一段代码,该代码将创建一个对象 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; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我右键单击 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>
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到:
异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“WebApplication4.Models.Class1+Customer”,但此字典需要类型为“WebApplication4.Models.Class1”的模型项。
ok,我把Class1.cs下嵌套的类拿出来了,
所以现在它只说公共类客户。
我还更改了命名空间 WebApplication4.Models
到 DefaultController1.cs 下的命名空间 WebApplication4.Controller,
但是当我进入 Index.cshtml 时,它说
命名空间 WebApplication4.Models 中不存在类型或命名空间 Class1
您的控制器正在实例化传递给 razor View 的类型的对象Class1.Customer(),而您的视图已被告知需要一个类型的模型Class1。
你有一个嵌套类 - 我不确定这是否是故意的?
如果是,请将视图更改为:
@model WebApplication4.Models.Class1.Customer
Run Code Online (Sandbox Code Playgroud)
编辑
我已经编译并运行它:
模型(/Models/Customer.cs)
namespace WebApplication4.Models
{
public class Customer
{
public int Id { set; get; }
public string CustomerCode { set; get; }
public double Amount { set; get; }
}
}
Run Code Online (Sandbox Code Playgroud)
控制器 (/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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
看法 (/Views/Default1/Index.cshtml)。
注意 razor 的使用方式与 Web 表单不同@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>
Run Code Online (Sandbox Code Playgroud)
产生以下页面: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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2938 次 |
| 最近记录: |