sarsnake
sarsnake

Reputation: 27703

How do I refer to ViewModel in my View

Ok, it may be a silly question, but how do I reference my ViewModel object that I passed to the View in asp.net mvc?

so say my controller action has this at the end

return View(myModelObject);

in my view, how do I refer to it? Please use razor syntax.

Upvotes: 1

Views: 108

Answers (3)

Mohamed.Radwan -MVP
Mohamed.Radwan -MVP

Reputation: 2744

You just need 2 steps

  1. use @model in the header of your view (with small letter m)

    @model MyNamespace.MyModelObject

  2. use the mode in your code inside a block or line statement as the following

Line

@Model

Block

@{
    Model

}

and remember you will use capital letter M

Upvotes: 2

David
David

Reputation: 218808

You can use @inherits of @model in the header of the view. Then, in the view's code, the object Model is a statically-typed reference to your model.

Upvotes: 1

user596075
user596075

Reputation:

You can reference it by the Model property of the ViewDataDictionary object. Something like this to reference your ViewModel's property:

@{string StringVariable = Model.YourViewModelStringProperty;}

Upvotes: 2

Related Questions