2

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
  • 2
    I think you've either misunderstood or typed that wrong. It's highly doubtful a view is using its own controller as the model.. can you double check that? Sep 9, 2012 at 20:47
  • I have updated my question - this is what I observed and I need clarification on this. It seems that only the user controls (.ascx files) have a model, but the .aspx pages that contain the user controls do not have a model, the markup in the .aspx pages (when you hover over C# code) indicates that the controller is being used as way of displaying data in the page.
    – Theomax
    Sep 9, 2012 at 22:12
  • Can you provide an example? How do the user controls get their model? Passed in by the view? How does the view get the model to pass to the usercontrol? Sep 9, 2012 at 22:35
  • Maybe your thinking of view-model? en.wikipedia.org/wiki/Model_View_ViewModel
    – NickSuperb
    Sep 10, 2012 at 3:48
  • When you say the ascx are using the controller as a way of displaying data, what do you mean? have you seen something like Html.RenderAction(Controller, Action)?
    – Manatherin
    Sep 10, 2012 at 16:24

2 Answers 2

3

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
  • He's probably confusing Model with ViewModel. Sep 10, 2012 at 15:34
  • @RobertHarvey - distinctly possible. Not quite enough info to fully understand what's taking place.
    – user53019
    Sep 10, 2012 at 16:01
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.