I have recently seen a .NET MVC solution in which the markup in the .aspx
views which appear to have a Controller as their model i.e the controller seems to be providing the data for the view, and the .ascx
user controls they contain use a separate model. I’m new to MVC and I wanted to find out about a few things I’m not clear on.
An example of how the code is implemented:
UserDetails.aspx
view has markup that shows it’s using the UserDetailsController.cs
as the model. It contains RenderPartial("User_Details.ascx", UserDetailsModel)
and passes it the UserDetailsModel
.
Is this the standard/correct way of implementing MVC? Or just one way to implement it?
I also noticed that the classes used as Models appear to be Service classes that have [DataMember]
and [DataContract]
attributes on the class name and properties – what is the advantage of this implementation?
6
Based upon the limited description so far, the best answer I can give is “maybe.”
Here’s my partial take on MVC, which generally aligns with others’ takes. But there’s always bones of contention to be had. I would recommend Martin Fowler’s articles as a good starting point with MVC and the other MVx variants.
The View should not be aware of the Model; it only knows what the Controller feeds it.
The Controller is more than welcome to directly pass a data structure from the Model to the View. But the Controller is on the hook to maintain that contract with the View if the Model should change.
In other words, the Controller may retrieve a List of users from the Model and then pass that List directly to the View. In this case, the structures magically aligned and all three layers are able to utilize the same (or similar) structure.
Should the View need additional details later on, then the Controller will be responsible for accessing the Model as necessary in order to provide those additional details back to the View.
2
The basic concept of MVC is:
Model: This is the domain model of your application.
View: This is the User Interface of your application. (you should not put any logic here.)
Controller: This is responsible for the interaction with the user. Pull data from ‘Model’ and select ‘View’ for the user interface.
For more details: http://www.asp.net/mvc/tutorials/older-versions/overview/asp-net-mvc-overview.
You can see the basic folder structure when you create a new ASP.NET MVC application in Visual Studio. Here Controlers are kept in one folder, Models are another and there is a view folder in which you can find many subfolders with the same name of the controllers. Under these folder you can find the views which the perticular controller will use. There is a ‘shared’ folder, under it has the views which can be usable for all the controllers. Microsoft create a convention over configuration concept in ASP.NET MVC. That is why the nameing convention and the folder stucture is essential.
[DataMember] and [DataContract] attributes are related with WCF.
Hope this help.