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.
posted 2 weeks ago
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
posted 1 week ago
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.
posted 10 hours ago
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
posted 6 hours ago
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: