0

I have developed in the past always classic ASP.NET solution. I am starting currently to develop ASP.NET MVC solutions, but i have a problem understanding what a Model is and what it should contain.

In the past i had an Object. For example one for User, Product and an object for Countries and one for Product Categories. Then within the class User i had a methods for retrieving a user, saving and updating it. The User objects properties where bound to Textboxes or labels on the .aspx page through codebehind code. Then the DropDowns for Country selection and the DropDown for Product Categories was also bound through the corresbonding objects.

Now my question on the M> Model in ASP.NET MVC. What should it contain and what not? I mean the Model as my understanding needs to hold ALL information which are needed to render the page. Does this means that i will have a List<Countries> for the DropDown boxes and also User.CountryId both in within the Model? And even the ErrorMessage needs to be added to the Model when i throw an exception and want to show it on the same page? Or am i wrong? What is the difference between Model, ViewData and ViewBag? When to use what? Questions over questions but maybe someone can enlighten me :)

asked Aug 10, 2016 at 19:29
3
  • 1
    This question is very broad, as it amounts to pretty much a complete class in MVC. As a starter, the original papers that introduced the term are very readable and accessible, and the original inventor of MVC keeps a nice reading list: heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html Commented Aug 10, 2016 at 19:39
  • In general i agree with you, but especially the Microsoft pages and the examples, tutorials on the web, are showing mostly a very very simple example which arent from my perspective enough to understand clearly how things act and are in real life. Therefore i asked this question and tried to narrow down the things with the third section of my question. Commented Aug 10, 2016 at 19:43
  • 2
    The Model is basically everything that is not a View or a Controller or client-side. What you're probably looking for is a ViewModel. Commented Aug 10, 2016 at 20:31

2 Answers 2

-2

The "M" in ASP.NET's MVC is better thought of as a "ViewModel", which is an object that contains everything your View needs to render information to the screen. So a ViewModel is sort of specific to one "page" of your MVC application (although you can certainly reuse them for common pages).

If you had a screen in your app, for example, that renders out a product's name, price, and description, and then has a text field to accept how many units a user wanted to order, the ViewModel might look something like

public class ProductOrderTaker
{
 // these will be filled out by data calls
 public string Name { get; set; }
 public string Description { get; set; }
 public decimal Price { get; set; }
 // these are exposed to the user for editing in the view
 public int UnitsToBePurchased { get; set; }
}

In your controller, in the initial GET loading of this page, you would new up an instance of this object, fill out the top 3 fields from a data call, and bind it as the Model for the View. The View would list the top 3 fields as labels (not form elements) and have a bound text field for the UnitsToBePurchased field. The POST version of the Action in the controller would accept this class as its parameter, and when it comes in, the UnitsToBePurchased field will have the value that the user filled out before posting the form.

That's essentially the gist of it. Your Model/ViewModel just has properties for anything that's being rendered from data calls, and other properties to store the user input from that screen. Typically they do not posses lots of Business Logic, but rather your DAL or Business Rule lib will hydrate them or validate them separately.

answered Aug 10, 2016 at 19:53
3
  • 2
    No. That's not what Model means. The ViewModel hangs off of the view, and provides separation of concerns between the View and the Model. Commented Aug 10, 2016 at 20:29
  • Yeah, but what the poster is asking for is definitely a ViewModel. Commented Aug 11, 2016 at 18:48
  • I'll remove my downvote. But Model and ViewModel are not at all the same thing; you should probably fix that in your answer. (I'm blocked from removing my downvote until the answer is edited) Commented Aug 11, 2016 at 18:50
2

Model Refers to a set of classes that describe tha data that the application works with.In addition,these classes define business logic that governs how the data can be manipulated.

answered Aug 31, 2016 at 10:07
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.