What are model and model variables for in a View?

Asked

Viewed 1,884 times

2

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

In my view I use it 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>
}

Man controller is like this:

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

It works properly, but I use it just because I’ve seen it like this, I still don’t 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 receive the variables listaEstabelecimento and teste?

2 answers

3


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

In case you have more than one type, a ViewModel 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 defined in @model of the kind EnvioParaView. Inside this object has two information to List<Estabelecimento> and the value string Teste.

References

3

@model is a directive of Razor, the mechanism of templates that forms the page on view. It is used to "type" the model.

The variable Model is linked to this statement, is what will use to access the data that comes from the call view, probably by controller. Is something implicit.

It is possible to declare other data on own view in other ways, which would not make much sense, except in very simple and specific cases, after all must do the minimum necessary in the view other than the presentation.

By way of curiosity your example is creating a class like this:

public class _Views_Account_Login_cshtml : RazorPage<List<Estabelecimento>>

I put in the Github for future reference.

then the variable Model be the type List<Estabelecimento> which matches what is being sent in View(listaEstabelecimento).

When accesses Model is actually accessing the class _Views_Account_Login_cshtml, all without you seeing.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.