Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different between @Model and @model

Basically I'm doing a test caused by one of excpetion.

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)
...

there will be an exception says:

CS1579: foreach statement cannot operate on variables of type 'UniBlue.Models.KYC' because 'UniBlue.Models.KYC' does not contain a public definition for 'GetEnumerator'

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

as regular HTML text

Can anybody tell me why this happened? What should I do to remove the strange title line?

like image 966
LifeScript Avatar asked Jul 19 '13 18:07

LifeScript


People also ask

What is difference between theory and model?

Theories are plausible explanatory propositions devised to link possible causes to their effects. Generally, models are schematic representations of reality or of one's view of a possible world, constructed to improve one's understanding about the world and/or to make predictions.

Which is correct modeler or modeller?

Modeler and modeller are both English terms. In the United States, there is a preference for "modeler" over "modeller" (88 to 12). In the United Kingdom, there is a 70 to 30 preference for "modeller" over "modeler". In India, there is a preference for "modeler" over "modeller" (67 to 33).

What does @model do in MVC?

The Model is the part of MVC which implements the domain logic. In simple terms, this logic is used to handle the data passed between the database and the user interface (UI). The Model is known as domain object or domain entity.

What is the difference between View and ViewModel?

VIEW: ( Platform Specific Code – USER INTERFACE ) What the user sees, The Formatted data. VIEWMODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Model and exposes it to the View. This is the model specifically designed for the View.


1 Answers

One is used to declare the strong type that the model is, and the other is used to access the model itself.

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 is a variable, @model is a keyword saying what type Model will be.

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>
like image 134
Matt Houser Avatar answered Sep 23 '22 11:09

Matt Houser