【发布时间】: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