johnny
johnny

Reputation: 19755

In an ASP.NET View, how do I access my Model's values in Razor and assign it to a variable?

In my view I have

@model Models.ViewModel.MyViewModel

I can get the values displayed with the Html.DisplayNameFor(model => model.my_id) method. This works fine.

But I cannot get the value of a field in the model by doing anything like

int id = model.my_id;

Every time I use "model," Visual Studio cannot find a reference to it. It does for the Html Helpers, but not when model is not in an Extension method like in the HtmlHelper (I thought it was an Extension Method).

I have tried many variations and various solutions I found on SO. I am missing something fundamental, but I cannot find the answer of how to get the reference to model and its values.

It is also causing this to fail,

 @Html.ActionLink("Edit", "Edit", new { id = model.my_id })

What am I doing wrong?

Upvotes: 1

Views: 26

Answers (1)

user1023602
user1023602

Reputation:

Use Model with a capital M.

@{
    int id = Model.my_id;
}

@Html.ActionLink("Edit", "Edit", new { id = Model.my_id })

Upvotes: 1

Related Questions