Question

I'm trying to pass my model to my view in Razor. With the old method, I could define it at the top of the file (the model).

I did some googling, and thought I figured it out - doesn't seem to be working. I'm not getting any intellisense on the model.

Top of the View file:

@Model CodySolution.Models.PhotoModel
@{
    ViewBag.Title = "Photography";
    Layout = "~/Views/Shared/_master.cshtml";
}

Where I'm using the Model:

<ul class="nav nav-pills nav-stacked margin-top">
    @foreach (var cat in Model.Categories)
    {
        <li class="active"><a href="#">@cat</a></li>
    }
</ul>

Is this the correct way to define it?

Était-ce utile?

La solution

@Model prints the value of the Model property.

To declare the model type, use the @model directive.

Autres conseils

In case someone is looking for the exact syntax, here it is:

@model CodySolution.Models.PhotoModel
@{
    ViewBag.Title = "Photography";
    Layout = "~/Views/Shared/_master.cshtml";
}

Note the lowercase @model since uppercase prints the value of the Model property.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top