• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Responsibility of model in .net MVC

 
Ranch Foreman
Posts: 1988
12
  • 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: 12481
269
  • 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 Foreman
Posts: 1988
12
  • 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: 12481
269
  • 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 Foreman
Posts: 1988
12
  • 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: 12481
269
  • 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.
 
Monica Shiralkar
Ranch Foreman
Posts: 1988
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.  Instead if ViewBag which other way should be used?The example which I am trying in my sample application is to read JSON data from the controller and show on the UI in a grid. I read that API controller is for such kind work. So I should change my MVC controller to api controller. What do I have to use repository for?
 
Stephan van Hulst
Saloon Keeper
Posts: 12481
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, ApiController is good if you want to return JSON to the client instead of a view.

If you want to return a view to the client instead of JSON, you would use a strongly typed view model instead of ViewBag, which you can declare in the Razor template using the @model directive.

You use repositories to transform data from the database to objects from your business model.
 
Monica Shiralkar
Ranch Foreman
Posts: 1988
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we use both API controller and MVC controller in same project, then what should be each of these be used for in the same project? Should API controller there be used only for where fetching of JSON is required to be shown on the front end and in all other cases we should be using MVC controller in the same project?
 
Stephan van Hulst
Saloon Keeper
Posts: 12481
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do anything with a regular MVC controller. ApiController just makes it easier to return objects that should be directly serialized to some format so that the client can decide how to display them, rather than being displayed in a web page that is still to be generated by the server.
 
Monica Shiralkar
Ranch Foreman
Posts: 1988
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my sample application I need to display a JSON with albums songs detail.  User shall enter textbox with albumn name and on clicking search button it shall display all songs from that albumn. I think I can call API controller for this purpose which takes a GET request with albumn name param and returns JSON with list of songs. I think I can use JQuery for calling the API controller. Is it fine to do this way?
 
Stephan van Hulst
Saloon Keeper
Posts: 12481
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, but why use jQuery?
 
Monica Shiralkar
Ranch Foreman
Posts: 1988
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I was thinking of using JQuery to reduce the amount of code to call the API from the HTML page.
 
Stephan van Hulst
Saloon Keeper
Posts: 12481
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Including a bloated library like jQuery in your project just to make some AJAX requests seems like overkill to me. Using XMLHttpRequest directly really isn't that much of a bother, and for the most common use cases you can just write a simple utility function.
 
Monica Shiralkar
Ranch Foreman
Posts: 1988
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks . yes that is an overhead.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Responsibility of Model in MVC
=> Model is responsible for handling data and business logic.
=> Model represents the shape of data in the project
=> Model is responsible for handling database and related changing
=> Model pass data into controller and controller pass data into view and represent to the user
 
Stephan van Hulst
Saloon Keeper
Posts: 12481
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ishan Shah wrote:Model is responsible for handling database and related changing


No it isn't. This is what the persistence layer is for. Sure, the persistence layer can have its own model, but that's not to mean that just any model is responsible for handling the database, or that the persistence layer doesn't exist of anything else but model classes.

Model pass data into controller


No it doesn't. Any proper model should have no idea of the existence of controllers. Models don't pass data to controllers, controllers pull data out of a model.
reply
    Bookmark Topic Watch Topic
  • New Topic