Jan Navarro
Jan Navarro

Reputation: 327

Does Razor View @model initialize the model?

I have a razor view which uses the @model keyword. I added a breakpoint in the model's constructor and whenever the view is rendered the breakpoint does not get called meaning it does not go into the constructor.

Upvotes: 1

Views: 2381

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

No. @model only declares the model type. If you need an instance of the model, then you must create one in your controller and pass it to the view, ie :

return View(new MyModel());

However, it's not always necessary to create an instance. This is only necessary if you need to access instance properties, such as a list of items that you need to iterate over.

Upvotes: 2

Related Questions