What are the model and model variables in a View?

2

What are the @model and Model variables in a view ?

In my view I use this at the beginning of the code:

@using Html5DataList.Models
@model List<Estabelecimento>

And to access the data, I use it like this:

@foreach (var item in Model)
{
    <option value="@item.unidadeId">@item.cnes - @item.nomeFanta</option>
}

My controller looks like this:

public ActionResult BPAC()
{
    List<Estabelecimento> listaEstabelecimento = new Estabelecimento().listaEstabelecimento();
    return View(listaEstabelecimento);
}

It works correctly, but I use it only because I've seen it like this, actually I still do not know what it means.

And, if I want to send more than one information? Ex:

public ActionResult BPAC()
{
    string teste = "teste";
    List<Estabelecimento> listaEstabelecimento = new Estabelecimento().listaEstabelecimento();
    return View(listaEstabelecimento, teste);
}

In this case, how do I get the variables listaEstabelecimento and teste ?

    
asked by anonymous 29.07.2017 / 15:45

2 answers

3

They are related, where @model defines the type of Model , in your code you set List<Estabelecimento> and used Model to access the positions of this establishment list . It is worth remembering that the @model directive was introduced in the ASP MVC Beta 3 version, whereas this directive defines the strong type of View in Razor .

If you have more than one type, a ViewModel is created containing these two types, as follows:

Example

ViewModel class

public class EnvioParaView 
{
    public string Teste {get;set;}
    public List<Estabelecimento> ListaEstabelecimento {get;set;}
}

Controller

public ActionResult BPAC()
{
    EnvioParaView model = new EnvioParaView ();
    model.Teste = "teste";
    model.ListaEstabelecimento  = new Estabelecimento()
                         .listaEstabelecimento();
    return View(model);
}

View

@using Html5DataList.Models
@model EnvioParaView 


@foreach (var item in Model.ListaEstabelecimento)
{
    <option value="@item.unidadeId">@item.cnes - @item.nomeFanta</option>
}


@Model.Teste

that is, the variable Model was set to @model of type EnvioParaView . Within this object you have two information: List<Estabelecimento> and value string Teste .

29.07.2017 / 15:55
3

@model is a Razor directive, the template engine that forms the page in the view . It is used to "type" the template.

The variable Model is bound to this statement, it will use it to access the data coming from the view call, probably by the controller . It's something implicit.

You can declare other data in view in other ways, which would not make much sense, except in very simple and specific cases, after all you should do the minimum necessary in view in> other than the presentation.

As a matter of curiosity your example is creating a class like this:

public class _Views_Account_Login_cshtml : RazorPage<List<Estabelecimento>>

then the variable Model will be of type List<Estabelecimento> that beats with what is being sent in View(listaEstabelecimento) .

When you access Model you are actually accessing the _Views_Account_Login_cshtml class, all without you seeing it.

If you want to send more than one information you have to create another model specifically for view to consume, usually through a class we call viewmodel (you can know more about this form in another question ). Already have some questions showing how to do this in ASP.NET MVC:

29.07.2017 / 15:55