@Model和@model之间的不同
基本上我做造成的错误时抛出的一个考验。
Basically I'm doing a test caused by one of excpetion.
通过使用返回查看(list_a
)的控制器,我通过一个列表进入我的看法,在我的视图页面时,code是这样的:
By using return View(list_a
) in controller I passed a list into my view, On my View page, the code is like:
@{
ViewBag.Title = "KYC_Home";
}
@using UniBlue.Models;
@model UniBlue.Models.KYC
...
@foreach(KYC a in Model)
...
会有异常表示:
CS1579: foreach statement cannot operate on variables of type 'UniBlue.Models.KYC' because 'UniBlue.Models.KYC' does not contain a public definition for 'GetEnumerator'
但是,当我改变了我的code到
@模型
网页看起来不错,但在标题就说明:
But, when i changed my code to @Model Page looks good but on the title it shows:
System.Collections.Generic.List`1[UniBlue.Models.KYC] UniBlue.Models.KYC
作为常规HTML文本
as regular HTML text
谁能告诉我为什么会这样?我应该怎么做才能去除怪题行?
Can anybody tell me why this happened? What should I do to remove the strange title line?
一是使用的模型是声明强力型,另一种是用于访问模型本身。
One is used to declare the strong type that the model is, and the other is used to access the model itself.
以下称用于模型的强类型为 UniBlue.Models.KYC
。
The following says that the strong type used for the model is UniBlue.Models.KYC
.
@model UniBlue.Models.KYC
这基本上宣告了变量模式
作为类型。这是类似于执行以下操作:
This basically declares the 'variable' Model
as that type. It's akin to doing the following:
UniBlue.Models.KYC Model;
模式
是一个变量, @model
是一个关键字,说什么类型的模式
将
Model
is a variable, @model
is a keyword saying what type Model
will be.
您的错误是因为你已经声明模式
为KYC,但KYC是不可枚举。你在使用它的foreach
期待一个的IEnumerable< UniBlue.Models.KYC>
这是不是这样的。
Your error is because you've declared Model
as KYC, but KYC is not enumerable. You're using it in a foreach
expecting an IEnumerable<UniBlue.Models.KYC>
which is not the case.
如果你的模型是一个真正的列表,然后使用
If your model is truly a list, then use
@model IEnumerable<UniBlue.Models.KYC>