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 :)
2 Answers 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.
-
2No. That's not what Model means. The
ViewModel
hangs off of the view, and provides separation of concerns between the View and the Model.Robert Harvey– Robert Harvey2016年08月10日 20:29:52 +00:00Commented Aug 10, 2016 at 20:29 -
Yeah, but what the poster is asking for is definitely a ViewModel.GHP– GHP2016年08月11日 18:48:43 +00:00Commented 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)Robert Harvey– Robert Harvey2016年08月11日 18:50:23 +00:00Commented Aug 11, 2016 at 18:50
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.
ViewModel
.