• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

responsibility of model in .net MVC

 
Ranch Hand
Posts: 1268
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning .net MVC and I learnt that:

1) Responsibility of Controller is to take the request from the browser , set the data to be displayed in a Viewbean object and return the view to the User.

2) Responsibility of View is to take the beanbag from the controller and display the data.

I did not understand What is the responsibility of Model because everything is covered in the above two ?

thanks
 
Saloon Keeper
Posts: 10875
235
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use the ViewBag. It's a weakly typed, well... bag... of stuff. Working with it is a big source of issues.

There are two kinds of model: The business model and the view model. The business model are the classes that represent your problem domain. Your controller gets your business model out of the persistence layer, and interacts with it to get the data it needs for that specific request. Then it builds up a view model that contains the data that is necessary to display the response to the client.

Here is a concrete example: say you have a student enrollment application that also keeps track of their courses and marks. The business model consists of all classes you need to just represent those concerns, without knowing whether your application will be a web application, console application or how the data will be stored. Examples of classes include Student, Course, Exam, etc.

Let's say the controller gets a request from the client to return a page that displays what marks a particular student got for all their exams. The controller retrieves the Student object and the associated Course and Exam objects from the database, and creates an object of type StudentMarksViewModel that encapsulates exactly all the data that is displayed on the response page. That object is the view model, and it is passed to the Razor template that extracts the data from the view model and insert it in the correct parts of the template.

You can access the view model from Razor using the @model directive.
 
Monica Shiralkar
Ranch Hand
Posts: 1268
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Apart from model thanks for explaining  that there is a business model and there is a view model.  I think that model and view exist separately just like the controller but the responsibility lies with the controller only to interact with the model and return the view. Model and view exist to be used by controller.
 
Stephan van Hulst
Saloon Keeper
Posts: 10875
235
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The business model exists to perform all calculations on the data.

The view exists to lay out requested information. It must not perform any logic.

The controller exists to accept requests, transform data from the business model into a view model, and route the transformed data to the correct view.

The view model exists only as a container of data that must be transported from the controller to the view, or from the controller directly to the client.
 
Monica Shiralkar
Ranch Hand
Posts: 1268
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is model tied to database always or model means from wherever the data comes from? Can I simply have such code in the controller to get data from the model.



dynamic modelData= MyModel.getModelData();

// code to pass modelData in the view

 
Stephan van Hulst
Saloon Keeper
Posts: 10875
235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the model must not know anything about the database, but the model can be retrieved from a database using the repository pattern.

At any rate, do not declare your variables as dynamic. Using dynamic variables is just begging for bugs. It's the same reason I told you not to use ViewBag.

Register your repositories in the OWIN pipeline when the application starts up. Then inject them in your controller classes. Your repositories can store models either in a database or in memory, that's up to you.
 
You didn't tell me he was so big. Unlike this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!