I’m learning asp.net mvc and found something interesting:
It seems that I can’t explicitly define a View’s Model from within the View with error message saying that it has no setter.
@{ this.Model = "Hello" } //error
Then I looked at the source code in WebViewPage.cs and a View’s Model property is actually like this:
public object Model {
get {
return ViewData.Model;
}
}
Thus the error.
But it’s interesting how I can do this: @{ ViewData.Model = "hello"; } and actually be able to use the @model statement, resulting to “hello”
I think I’m looking too much into it, but why is this so?
beginner at C# and ASP.NET
The rule is Separation of Concern…In MVC, a Controller supplies a Model to a View and it will always be the controller that can set/assign a Model to a view….which the Views can use…this is by design…play by rules is what I would say…and If you are learning MVC its great and I would strongly recommend you to read
Stevens Sandersons MVC book