How to set Model for Strongly Typed View in Razor

Cody picture Cody · Jan 17, 2013 · Viewed 25.8k times · Source

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?

Answer

SLaks picture SLaks · Jan 17, 2013

@Model prints the value of the Model property.

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