I am new to MVC5 and Entity Framework.
Scenario is this
I have to read from a already existing database
Only SELECT statements are used to fetch data.
This is shown as tables/reports in View
I don't need a model, Since the database is already designed and populated with data. I only need to use the SELECT statement to Query and show data.
A model requires KEY which is not needed in my scenario.
Can I create DBContext and use the context in controller to read the data to list and show in View
Any help would be appreciated
MVC stands for (Model View Controller) with separation of concerns for data.
Model:
An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder.
A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.
In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.
VIEW:
The two controller actions exposed by the HomeController class, Index() and About(), both return a view. A view contains the HTML markup and content that is sent to the browser. A view is the equivalent of a page when working with an ASP.NET MVC application.
You must create your views in the right location. The HomeController.Index() action returns a view located at the following path:
\Views\Home\Index.aspx
The HomeController.About() action returns a view located at the following path:
\Views\Home\About.aspx
In general, if you want to return a view for a controller action, then you need to create a subfolder in the Views folder with the same name as your controller. Within the subfolder, you must create an .aspx file with the same name as the controller action.
CONTROLLER:
A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.
A controller is just a class (for example, a Visual Basic or C# class). The sample ASP.NET MVC application includes a controller named HomeController.cs located in the Controllers folder. The content of the HomeController.cs file is reproduced in Listing 2.
Here is a good Video about Understanding MVC. http://www.asp.net/mvc/videos/mvc-2/how-do-i/understanding-models-views-and-controllers
Related
I have gone through some answers here and and some articles on MVC but I still have a confusion regarding the role of a Controller in a MVC application.
I read in a book that the Model is self-contained and functions independent of View and Controller. And that the Model contains the business logic and data access codes. Source
Also in the best answer
here
What goes into the "Controller" in "MVC"?
But the other answers around here say that it is the Controller that represent the business logic
Where can I find a dead-simple explanation of MVC?
So which is the correct answer?
Wikipedia states very simply: The controller, accepts input and converts it to commands for the model or view.
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
This answer is correct
Model: This component represents the data and the business logic of the application.The model in the MVC framework is not related to the presentation of the application.The model component focus on keeping track of the state of the application.It also defines business rules for data, means how the data can be changed and manipulated.
View: The view provides the user interface (UI) for the model. The main work (function) of the view represents the information in user understandable format. It uses UI Components such as HTML,CSS,Jquery etc.
Controller: Controller act as a mediator between view and model. it is responsible to control the data transmission between the model and the view. It maps the user action into model updates.The controller layer is helpful to select the most appropriate view and delivers it to the user.
Actually, The Controllers are mediater between the view and model. they don't define the business logic. Models are responsible for business logic.
Controller acts as a mediator between the View and the Model class. It is used just to remove the dependency between model and view classes.
Kindly refer to this link..
https://www.tutorialspoint.com/mvc_framework/mvc_framework_controllers.html
The ASP.NET MVC framework maps URLs to classes that are referred to as controllers. Controllers process incoming requests, handle user input and interactions, and execute appropriate application logic. A controller class typically calls a separate view component to generate the HTML markup for the request.
The Controller class is responsible for the following processing stages:
Locating the appropriate action method to call and validating that it
can be called.
Getting the values to use as the action method's arguments.
Handling all errors that might occur during the execution of the action method.
Providing the default WebFormViewEngine class for rendering ASP.NET page types (views).
I'm fairly new to the .net Framework and the whole MVC programming philosophy. Could someone clarify and give me a basic explanation how controllers interact with sites using C#? I understand how to code in C#, and I understanding some aspects of the framework, but I don't see how they all tie together.
Model - Is a data structure that represents some kind of object (usually one). It's purpose is to read, write and manage the access to the underlying object with the aim to persist application state.
View - Is the components that are used to display a visual interface to the user, perhaps using a model. It might be a simple table or a complex combination into a full web page.
Controller - Is the user driven application logic layer the sits between views and models. It handlers user interaction, loads models, and sends views to the user. It determines what model is sent to the view depending on user requests.
The overall folder structure for an application might look like this.
>> Website
>> Controllers
>> Models
>> Views
In C# MVC each controller must have the suffix Controller in the name, they must extend Controller class and have a folder of the name prefix (without the Controller) in the views folder. This folder will then contain all the views related to particular actions on the controller.
Controllers can contain any number of actions defined as public functions. By default when returning a result from a controller action the name of the view must correspond with the name of the action. However you can also specify a view by name. When loading a view from a controller, it is possible to send an object as a model to the view and there by generate it's content.
Controllers can load any model and are not restricted in any way.
An Account controller defined as below with an action Login. The controller is placed in a AccountController.cs file in the /Controllers folder, and any views for this controller (Login in this instance with filename Login.cshtml) are placed in the /Views/Account folder.
Note: The naming convention has to be right as the names are used between the controllers and views to link the data.
public class AccountController : Controller
{
public ActionResult Login(string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Index","Site");
}
return View("Login", new LogOnModel());
}
}
would be accessible via http://www.mysite.com/Account/Login. If the user is authenticated, the controller will redirect to the main site controller, if the user is not logged in then they are shown the Login view which loads data from the LogOnModel specified.
This is really just touching the surface of what is possible. Read some online information on some excellent articles by ScottGu which go into much more depth and talk you through how to use MVC.
ASP.NET MVC Framework Overview
ASP.NET MVC Framework How To - Part 1
// Part 2
// Part 3
// Part 4
Note : These articles are slightly outdated as they were written for MVC version 1 back in 2007, but the concepts of how the Models, Views and Controller interact still apply.
Controllers serve somewhat as an internal web service. They expose your server side code to your views and allow them to call the controllers. In terms of pattern, most people believe that controllers should be as thin as possible. If there is heavy lifting or other business logic, you should abstract it to another part of your application. In my eyes, controllers are there to provide the view with something to call, and then to return that data whether it be text/html, json, or xml.
Here's a great wealth of information, straight from the source: http://www.asp.net/mvc/mvc4
Specifically to the site, I'd highly recommend the tutorial. It will give you a much clearer picture of how Models, Views, and Controllers interact and depend on one another.
http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4
A controller is a class which has methods, those methods are called actions, you atach those actions to "views" (cshtml files).
//This is your controller
public class HomeController : Controller
{
// This is your action
public ActionResult Index()
{
return View();
}
}
You can right click the "Index" action and select "Add View..." this will create a new view atached to that action.
In order to access that view you will do something like: localhost/Controller/Action
In this case it should be: localhost/Home/Index where Home = Controller, Index = Action
You should read about the MVC pattern
Microsoft has some really good tutorials for beginers
Controller in ASP.NET MVC is an object that handel your app logic in response of requests. It will be created per request (e.g. a HTTP request) and will be available until response created by View layer. After that it will be an unusable object (and soon GC will free its allocated memory) and for another request a new controller object must be created and so on.
I think by this definition it will be obvious why it must be lightweight and how you must use it.
Can you explain to me the point of a controller when all it does it return a view? Have I missed the point here?
I've come across a situation when trying to build a rudimentary CMS where the user can create views (stored in the database), but of course as they are user created, the controller's don't exist. So is there another way to serve them?
Thanks for any help, I'm still trying to get to grips with MVC fully!
Can you explain to me the point of a controller when all it does it return a view?
Who said that all a controller does is to return a view? A controller does lots of other things. For example it could receive the user input under the form of an action parameters, check whether the ModelState.IsValid, do some processing on the Model and then return a View which is the whole point of the MVC pattern.
I've come across a situation when trying to build a rudimentary CMS
where the user can create views (stored in the database), but of
course as they are user created, the controller's don't exist. So is
there another way to serve them?
Yes, of course. You could use a custom virtual path provider by implementing the VirtualPathProvider class.
I've been doing some research on using ASP.NET MVC and Entity Framework together for a fairly large project.
Most examples separate the .edmx file from the MVC project by moving it to a new DAL project. In this project you would also find repositories and interfaces.
While this approach makes perfect sense to me, there's one thing I can't seem to figure out: what about the Models in MVC? In most examples the Controllers address the repository interfaces from the DAL project directly, so the MVC Models are no longer used? Or is it a good idea to keep using them, but map them in the Controller?
There are 2 types of models:
domain models
view models
The domain models represent your domain entities. They could be the autogenerated EF classes from your database or coming from somewhere else such as proxies generated from a WCF service that you are consuming. Those should live in your domain layer.
View models on the other hand go in the Models folder in the ASP.NET MVC project. Those are specific classes that you define for each view. The controller actions will query your DAL layer to fetch one or more domain models and instantiate a view model which you have specifically defined for the given view that you want to render from this controller. So a view model could hold information from multiple domain models (just because in the given view you need all this information). Then the controller passes the view model to the view for displaying.
As I've been reading this article: http://www.codeproject.com/Articles/42830/Model-View-Controller-Model-View-Presenter-and-Mod, MVC was explained like this:
In this diagram, View-Model is observer pattern where any changes in Model notify View's Update method to update the states of Views and passing its current state to these Views.
Relation between Views and Controllers are defined in Strategy Pattern where Views get concrete controllers and run the algorithms in controllers as defined by Strategy Pattern.
So Controllers update models and models notifies views to update their states.
However, for some reason I couldn't visualize the same logic for Asp.Net MVC as Controllers have access to Model and pass these models to Views but views uses Models directly to get their properties,etc to update their states but I couldn't fit the observer pattern between Views and Models in Asp.Net MVC.
Could some one please explain what I am missing out?
ASP.NET MVC upholds the stateless nature of the web, so once a view is rendered there is no way for the controller to know about changes to an instance of a model object.
This is different to a stateful platform like Silverlight or WPF. The fact that they're stateful allows a controller (or ViewModel if you're using MVVM) to observe what's happening on the UI, make changes to model objects, and then have the view update itself accordingly.
In ASP.NET MVC when data gets passed from the controller to the view, it's a one-way trip. There's no equivalent of the View.Update() method; the view gets rendered once and then that's it. There's also no way for a view to call back to a controller, as per Controller.AlgorithmInterface. You can achieve something similar with an AJAX callback but then you'll have to use some behind-the-scenes magic to repopulate the controller with the state data (e.g. by passing an object ID from the view back to the controller).