Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Model for Strongly Typed View in Razor

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?

like image 963
Cody Avatar asked Jan 17 '13 01:01

Cody


2 Answers

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.

like image 40
Joshcodes Avatar answered Sep 16 '22 18:09

Joshcodes


@Model prints the value of the Model property.

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

like image 140
SLaks Avatar answered Sep 20 '22 18:09

SLaks