Action Selectors in ASP.NET MVC
Hello friends, I hope you all are having fun with your lives. In today's tutorial, we are gonna have a look at Action Selectors in ASP.NET MVC. It's the 11th tutorial in ASP.NET MVC series and if you have covered so far then that means you really want to learn this language. Action Selectors are also new for you as we haven't discussed them yet but they are not that difficult to understand.
Action Selectors are built-in attributes of ASP.NET which are directly applied to action methods of Controllers. We use Action Selectors to influence on Controller's action methods. In simple words, we use Action Selectors to guide URL Routers in selection of correct action method. Let's have a detailed overview of Action Selectors:
Action Selectors in ASP.NET MVC
- Action Selectors are built-in attributes provided in ASP.NET MVC and are directly influence our Controller's action methods.
- Action Selectors guide the URL Routers to respective action method of Controller which then opens up View for the user.
- Action Selectors are simple C# classes.
- There are 3 types of Action Selectors available in ASP.NET MVC, named as:
- ActionName
- NonAction
- ActionVerbs
- So, let's discuss each of them in detail:
1. ActionName Selector in ASP.NET MVC
- ActionName Selector (as the name suggests) is used to change the name of Action from the name of Controller's Method.
- Let's open the StudentController.cs file which we created in Tut # 06: Create a New Controller in ASP.NET MVC.
- I have added an action Method here Get() and above this method I have placed [ActionName("GetAction")], as shown in below figure:
- If we want to access this action method Get(), then we have to place localhost:12345/Student/Get in our browser.
- But after placing the ActionName Selector, now we have to use localhost:12345/Student/GetAction in order to call Get() Method.
- So, in this case the name of action is GetAction but the name of method is Get.
- But if we don't use ActionName Selector, then name of both Action and Method is Get.
- Let's have a look at what's the effect of ActionName Selector:
- As you can see in above figure that when we have used Get then we received Server Error.
- When we have used GetAction (assigned in ActionName) then our Method Get() is called.
- Here's the video demonstration of ActionName Selector in detail:
2. NonAction Selector in ASP.NET MVC
- NonAction Selector is another built-in attribute of ASP.NET MVC and as the name name suggests, it is used to remove the Action from action method.
- If you place NonAction Selector before an action method in your Controller then you won't be able to access that method directly from its action.
- URL Router will think this method doesn't exist, if its called directly.
- Let's add some code in our StudentController.cs file, as shown in below figure:
- I have added [NonAction] before our action method Get1(), that's how we specify nonAction Selector in ASP.NET MVC.
- If you have a look in the Index Method then you can see I have called Get1 there.
- Now when we open the Home Page of our site, then this Get1 action method will be called and it will work fine.
- But if we want to call the Get1 action method directly then it won't open up and will give us a Server error.
- That's because we have placed nonAction Selector before it.
- Results of both Home page & direct calling the action method are shown in below figure:
- So, its cleared from the above figure that nonAction has removed the action from our method and now its just a simple public method, which we can call from inside but no one can access it from outside directly.
- Here's the video demonstration of nonAction Selector, must watch it for live action:
3. ActionVerbs Selector in ASP.NET MVC
- ActionVerbs Selector is third built-in selector in ASP.NET MVC and is used to assign some specified HTTP request to action methods.
- It helps the URL router in selection of correct action method.
- Let's say you have two action methods in your project and they have a same name but you want to use one for GETting data and the second one for POSTing data.
- Let's have a look at the similar situation in below code:
- AS you can see in above figure that we have written two action methods of the same name Get1.
- So, now if you call it in your browser then you will get Server Error, as shown in below figure:
- So, now let's place our ActionVerbs Selector before one of these action methods, which one we want to call.
- I have placed the [HttpGet] before second action method and now because of this ActionVerbs Selector,URL Router will know which action method to call.
- Now when you open Get1 page, it will open fine.
- These ActionVerbs Selector are supported by ASP.NET MVC:
-
- HttpPost
- HttpGet
- HttpDelete
- HttpPut
- HttpOptions
- HttpPatch
- Here's the video demonstration of ActionVerbs Selector in detail:
So, that was all about Action Selectors in ASP.NET MVC. I hope you have enjoyed today's tutorial and now can easily use Selectors in ASP.NET. Thanks for reading. Have a good day !!! :)
Create a Custom Filter in ASP.NET MVC
Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at How to Create a Custom Filter in ASP.NET MVC. It's our 10th tutorial in ASP.NET MVC series. In our previous tutorial, we have cleared our concepts about
Filters in ASP.NETMVC and we have also implemented one built-in filter of ASP.NET MVC.
Now, its time to have a look at How to Create a Custom Filter in ASP.NET MVC, in which you can specify your own authentications or handling etc. In our ASP.NET applications, we have use custom built filters a lot for different purposes. So, let's have a look at How to Create a Custom Filter in ASP.NET MVC:
Create a Custom Filter in ASP.NET MVC
- In ASP.NET MVC, custom filters are handled by a base class named as ActionFilterAttribute.
- This ActionFilterAttribute class implements both IActionFilter & IResultFilter.
- Although ASP.NET MVC has numerous built-in filters but in many cases, there comes a need for custom rules or handles.
- In such cases, we need to design our custom Filters using ActionFilterAttribute.
- In your Solution Explorer, right click on the Main project and click on Add and then click on New Folder.
- It will create a New Folder in your project, rename it to ActionFilter.
- Now, right click on this ActionFilter folder and click Add and then Class, as we want to add new class in it, as shown in below figure:
- It will open up a new pop up window, where you need to select class and then give it a name.
- I have given a name TEPCustomActionFilter.cs to my Filter class file, as shown in below figure:
- Now click the Add Button and a new class will be added in your ActionFilter folder and will also open up in your workspace.
- It will have some default code in it, as shown in below figure:
- Let's add some code in our newly created filter class, as shown in below figure:
- We need to derive this class from ActionFilterAttribute that why I have extended the class name.
- We need to include System.Web.Mvc in order to use ActionFilterAttribute.
- In the class, I have created a simple function in which I am displaying a text on the output debug pane, after the execution of my project.
- So, we have created our custom filter, now it's time to add it in our StudentController which we have created in Tut # 6: Create a new Controller in ASP.NET MVC.
- Open your StudentController.cs file and place this filter in it, as shown in below figure:
- First of all, we need to add our package TEPwebApp.ActionFilter, ActionFilter is the name of our folder we created for our custom filter.
- Now before the StudentController class, place the code for our newly created custom Filter [TEPCustomActionFilter].
- Now run your project and after website opened in the browser, stop your project and check your Output Pane.
- You will find the text, as shown in below figure:
- I have highlighted our Text in the above figure.
- So, our filter has executed after the StudentController class and it has displayed the info.
- Such Filters are used for listening purposes, we can listen different types of data through them.
- Have a look at this video to get better understanding of How to Create Custom Filter in ASP.NET MVC:
So, that was all about How to Create a Custom Filter in ASP.NET MVC. I am just focusing on the basic details here, we need to clear our concepts first and with practice we can become pro. Will meet you guys in the next tutorial, take care and have fun !!!
Filters in ASP.NET MVC
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna have a look at
Filters in ASP.NET MVC. It's our 9th tutorial in ASP.NET MVC series. It's also a new concept which is essential to discuss as it's used almost in every form submission.
We have already understand Controllers in ASP.NET MVC and we know that Controllers have action methods in it which are directly assigned to respective Views, which are then displayed to the user. But in some cases, we have to place some checks (called Filters) before or after this action method called. So, let's have a look at these Filters in ASP.NET MVC in detail:
Filters in ASP.NET MVC
- Filters are attributes which we can use before or after action method called in a Controller.
- Controllers are normally directly connected with their respective Views in the action method, but in some case we need to place some logic i.e. user authentication or IP address verification etc. before the View is displayed.
- In order to handle such intermediate logic or verification, ASP.NET MVC uses some Filters.
- Filters are simple custom classes where we can write our code and can assign different attributes or verification to our action method in Controllers.
- Let's have a look at the types of Filters supported by ASP.NET MVC:
Types of Filters in ASP.NET MVC
- ASP.NET MVC framework offers four types of Filters.
- Depending on the type of filters, they may occur before or after the action method.
- Let's have a look at these Filters and their description:
Types of Filters in ASP.NET MVC |
No. |
Type |
Description |
Interface |
1. |
Authorization .Filters |
These Filter performs some user authorizations or verification before action methods. |
IAuthorizationFilter |
2. |
Action Filters |
These Filters assign certain actions before or after methods. |
IActionFilter |
3. |
Result Filters |
These filters handles the View results and assigns some actions to it. |
IResultFilter |
4. |
Exception Filters |
These filters control the exceptions, if occurred during execution. |
IExceptionFilter |
- These four Filters always occur from top to Bottom as given in above table i.e. Authorization Filter will run first and action Filter will run afterwards and so on.
- This order can't be reversed or changed.
- ASP.NET MVC has some built-in Filters designed in it, some most commonly used built-in Filters are:
Built-in Filters in ASP.NET MVC |
No. |
Name |
Description |
Filter Type |
1. |
OutputCache |
This Filter stores / caches the web data for a specified amount of time. |
Result Filters |
2. |
HandleError |
This Filter handles different exceptions or errors generated during execution of MVC Application. |
Exception Filters |
3. |
Authorize |
This Filter is used for user verification or authorization before the action method. |
|
- Now let's have a look at How to use OutputCache Filter in ASP.NET MVC:
OutputCache Filter in ASP.NET MVC
- We will cover all these filters in our coming tutorials, as they will be required all along, but for now we will have a look at How to use OutputCache Filter for understanding the Filter concept.
- As I have mentioned earlier OutputCache is a built-in filter and it is used to store the web data for a specified time.
- It helps in saving the load time, suppose you are updating your site continuously for some live data, then you can place some checks like if a user refresh the page before 10 sec then upload the page from cache.
- In such cases you can use OutputCache, let's implement this logic in our StudentController, which we have created in Tut # 06: Create a New Controller in ASP.NET MVC.
- So, open your StudentController File and add the code in it, as shown in below figure:
- You can see in the above figure that I have created a Get method and I am simply displaying the current Date and Time on the page.
- Above this Get method, I have also added OutputCache Filter and I have assigned it a duration of 10 sec, so it will cache for 10 seconds.
- Now remove this Output Cache line, and refresh your page and on each refresh the time will change and it will get the current date and time.
- Now let's add this OutputCache line and give it a duration of 10 sec.
- If you refresh your page now, the time will not change, but if you refresh it after 10 seconds then it will change and get the new date and time.
- Here's a screenshot of our Student/Get page:
- Now you have seen we have placed the Filter code before action method so this OutputCache filter is executed before our Controller's action method.
- Have a look at this video, which will give you better understanding of OutptuCache:
So, that was all about Filters. I hope you have understood the basic concept of filters and have an idea about working of built-in filters. In our coming tutorial, we will have a look at How to create Custom Filters in ASP.NET MVC. So, will meet you in next tutorial, take care & have fun !!!
URL Routing in ASP.NET MVC
Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at
URL Routing in ASP.NET MVC. It's the 8th tutorial in this ASP.NET MVC series. Today's tutorial is on a new concept in asp.net and it's essential to understand URL routing before going any further.
URL Routing is actually a part of core ASP.NET but MVC applications also use it. URL Routing is implemented by System.Web.Routing and ASP.NET MVC uses System.Web.Routing. So, let's have a look at what's URL Routing in ASP.NET MVC:
URL Routing in ASP.NET MVC
- URL Routing is used for directing the HTTP request (generated by the user) to the respective controller in ASP.NET MVC.
- Whenever a user types some url in the browser and hit enter then an HTTP request is generated and this HTTP request is then handled by the controller.
- This assigning of HTTP request to controller is done by URL Routing.
- In ASP.NET MVC application, a default Routing file is automatically created in the project.
- In your Solution Explorer, Click on the folder App_Start.
- In this folder, you will find a file named RouteConfig.cs that's our URL Routing file, all our settings are saved in it.
- So, open this file in your workspace, as shown in below figure:
- Now you can see this RouteConfig.cs file in above figure, it has some default code in it.
- Let's have a close look at this code and understand it:
- In this file, we have a MapRoute extension method of RouteCollection, which is a property of RouteTable class.
- In this MapRoute method, we have three lines of codes, This code is actually setting the URL for Home Page i.e. the index file will open up when you open Home Page.
- First line is name of our Route and in this case its Default.
- The second line specifies the URL parameters and you can see it specifies the url syntax, first we have the Controller, after that action from that Controller, and the third parameter is id.
- So, actually we want to assign the URL request to some Controller and then assign a action from that controller.
- In the 3rd line, we have assigned default parameters to our variables in URL.
- So, the Controller we have here is Home and the action we have assigned is Index and id is optional for now.
- Now if you recall from our previous tutorial Controllers in ASP.NET MVC that in our HomeController file we have a method called Index, as shown in below figure:
- So, I hope you have understood How this URL Routing is working.
- Let me summarize it, by default we have a URL Routing File and it has the MapRoute extension method for the Home Page and from HomeController file index action is assigned to the URL.
- This index action is further assigned to index View and thus it open the index page.
- Now, let's say you want to change your Home Page from index to About.
- So, you need to do these changes in the Route file:
- As you can see in the above code that I have just changed the action from index to About.
- If you remember, our HomeController also has another method About, which is assigned to About View.
- So, now my URL Route will open the Home Controller and then will run the About method and in turn About View will open up for the user.
- Now when you open the Home Page of your site then About Page will be open up.
- Here's the video demonstration of URL Routing ASP.NET MVC:
So, that was all about URL Routing in ASP.NET MVC. I hope you have enjoyed today's tutorial. If you have any questions, then ask in comments. Will meet you guys in the next tutorial, take care !!!
Create a New View in ASP.NET MVC
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna have a look at How to Create a New View in ASP.NET MVC. It's 7th tutorial in ASP.NET MVC series and I am hoping you are learning from it. If you have any suggestions, then ask in comments and we will help you out.
In our previous tutorial, we have seen How to Create a
New Controller in ASP.NET MVC and we have also created the
New Model in ASP.NET MVC , so now we are only left with View which we are gonna create today. Both Controller and Model comes under back-end / server side programming while view comes under front-end / client side programming. Here we are gonna create a View for the user to interact. So, let's get started with How to Create a New View in ASP.NET MVC:
Create a New View in ASP.NET MVC
- We have already seen How to create Models and Controllers, and now we need to create Views in ASP.NET MVC so that we could interact with our user.
- When a user sends a http request, then controllers get data from models and then display them on Views.
- So, in order to create a new View, you have to open the StudentController.cs file, which we have created in the last tutorial.
- Now in this file, right click on the Index in the code and then select Add View, as shown in below figure:
- When you click on Add View, it will open up a new window named "Add New".
- This window is used for adding new View in your project.
- We have to set some values in this window and then click on the OK button, as shown in below figure:
- As you can see in the above figure, first of all, we need to give it a name, which I have used "Index".
- In the Template section, I have selected "Create" template as I want to create a registration form.
- In the model class, we have to select the model, with which we will assign this View. So, here I have select the StudentModel, which we have created in our 5th Lecture.
- After these settings, click the Add button and in your Views folder, a new folder will open up named Student linked to StudentModel.
- In this Students folder, a new index.cshtml will be created and it will also open up in the workspace, as shown in below figure:
- Run your project and add /Student after your base name in the url, as we have placed this index file in the Student folder.
- When you open this link , the default index view page will open up, as shown in below figure:
- Now let's make some changes in the index file and get data from the Student Model.
- I have added two lines of code, the first line is creating a label and getting the text from our model class.
- In the second line, we have created the textbox, as shown in below figure:
- I have removed the code for the Create Button, and added these two lines.
- Now let's refresh our page, and if everything goes fine then you will get results as shown in below figure:
- That's our final Index page for newly created View.
- Now, let's keep it here for now and let's discuss some more concepts in ASP.NET MVC.
- Till now we have seen what are Models, Views & Controllers and then we have also seen How to create them.
- Here's the video demonstration of How to Create a New View in ASP.NET MVC:
I hope you have enjoyed today's tutorial. In our next tutorial, we will have a look at URL Routing in ASP.NET MVC. Take care & have fun !!! :)
Create a New Controller in ASP.NET MVC
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna
Create a New Controller in ASP.NET MVC. It's 6th tutorial in our ASP.Net MVC series. We have already had a look at basic Introduction to
Controllers in ASP.NET MVC and they are used to communicate between Views & Models.
So, now we are gonna have a look at How to Create a New Controller in ASP.NET MVC. It's a very quick tutorial, as there's not much to do with Controller rite now, we will just create one. So, let's get started with it:
Create a New Controller in ASP.NET MVC
- In order to Create a New Controller in ASP.NET MVC, you have to right click on the Controllers folder in Solution Explorer.
- Now click on the Add and then click on Controllers as shown in below figure:
- It will open up a new window which will have different types of Controllers and are used for different purposes.
- Here, we have to select the second one as we want to perform read/ write actions.
- So, select MVC 5 Controller with read / write actions and then clock Add Button, as shown in below figure:
- When you click the Add Button, it will ask for the Controller name.
- I have assigned it a name StudentController, now click the Add button, as shown in below figure:
- Now when we click the Add button, then a scaffolding operation will start by visual studio and it will automatically create our new controller.
- This new Controller file will also open up in our work space and it will have some classes by default in it, as shown in below figure:
- We have successfully created our new Controller to read or write data.
- In our previous lecture, we have already created a new Model in ASP.NET MVC.
- So, now we are only left with the Views which we are gonna create in our next tutorial and will also bind them together.
- Here's a video demonstration of How to Create a new Controller in ASP.NET MVC, it will help for better understanding:
So, that was all about How to Create a New controller in ASP.NET MVC. If you have any questions, then ask in comments and I will surely help you out. Thanks for reading. Take care.
Create a New Model in ASP.NET MVC
[vc_row full_width_row="true"][vc_column][vc_column_text]
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna have a look at How to
Create a New Model in ASP.NET MVC. It's 5th tutorial in ASP MVC series. We have already seen the basic concept of Model in ASP.NET MVC in our second tutorial. Models are used to communicate data from SQL Database to respective Controllers, which is then displayed by the Views to the user.
When you are designing some professional website then it happens a lot that you need to place different data in your databases, normally this data is coming from users. So, in such cases you have to create new models and then assign respective controllers to them. So, today we will see How to do the first part i.e.
creating new models in asp.net mvc.
Create a New Model in ASP.NET MVC
- Models in ASP.NET MVC are simple C# classes and if you look at their file extension then it's .cs, so we just need to create a new C# class file.
- So, in your Solution Explorer, right click on the Models folder.
- Now click on the Add and then click on Class, as shown in below figure:
- When you click on the Class, it will open up a new window.
- In this new window, you need to select Class and then write some name for it, as I have given it a name StudentModel.cs.
- Finally, click the Add Button, these settings are shown in below figure:
- When you click on this Add Button, visual studio will automatically create a new Model file in your Models Folder.
- This new Model File will also open up in your workspace and will have small default code in it, as shown in below figure:
- Now I will create some attributes in my new Model File, where I ask for some basic registering details from my students.
- I will use them later after creating a new Controller & View for this model, but rite now we are just doing the server side programming, there's no client side involved.
- So, let's have a look at the Model Code:
- You can see in above figure that I have created four variables inside my new StudentModel class.
- All these variables have a datatype of string and are initialized with two further attributes get & set.
- When we need to get value from SQL, then we use get & when we want to save some value then we use set.
Note:
- I am not sharing any codes because we are in initial phases & it's more about understanding the flow of coding.
- I will start share the code once we start writing it, rite now there's not much coding part.
- Let's have a look at the video explanation for better understanding:
So, that was all about Creating new Model in ASP.NET MVC. In the coming lecture, we will create new Controller in ASP.NET MVC for this model. So, take care & have fun !!! :)
[/vc_column_text][/vc_column][/vc_row]
Controllers in ASP.NET MVC
Hello friends, I hope you all are doing great. In today's tutorial, I am going to give you a detailed overview of
Controllers in ASP.NET MVC. It's the fourth article in ASP Web App Series. In our Previous tutorial, we have had a look at
Views in ASP.NET MVC and in that tutorial, we have mentioned this Controller a lot and today we are gonna discuss it in detail.
Controllers acts as a messenger in ASP.NET MVC Web App, it takes message or data from place to another. I have also shared a video at the end of this tutorial and I would suggest you to watch it once to get better understanding of controllers. So, let's get started with Controllers in ASP.NET MVC:
Controllers in ASP.NET MVC
- Controller is the back bone of any ASP.NET MVC Web App as it acts as a messenger between different entities.
- When use hit your website address in the browser then browser generates an HTTP request, which is received by the server and in ASP case it comes to Controller.
- Controller in ASP.NET handles the coming HTTP request and then selects the respective Model in ASP.NET MVC and then gets data from the SQL Database, if required.
- Along with this data from Model, it forwards the command to respective View, which in turn opens up in front of the user.
- So, you must have got the importance of Controllers as they are kind of doing the management work, getting and assigning commands & data.
- Controllers are inherited from System.Web.Mvc.Controller and are simple C# classes.
- You can find the Controllers folder in Solution Explorer, click to expand, as shown in below figure:
- You can find 3 Controller files in this folder, named as:
- AccountController.cs
- HomeController.cs
- ManageController.cs
- So, now click on the Home Controller file and let's have a look at the code, shown in below figure:
- In the above code, we have used a method named ActionResult, which will send the data to View.
- After the name of method, the name of each page is placed present in the Views > Home folder.
- Now let's open the About Page and the string variable is shown on the About Page, as in below figure:
- If you open the About View file, then you will see this string variable is placed there, that's why we have it on screen.
- We can create unlimited controllers and can use them for different purposes.
- Here's a video which will give better understanding of Controllers in ASP.NET MVC:
So, that was all about Controllers in ASP.NET MVC. I hope now you can easily understand the difference between Models, Views & Controllers. In coming lectures, we will have a look at How to create New Models, Views & Controllers in ASP.NET MVC. Take care & have fun !!! :)
Views in ASP.NET MVC
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna have a look at What are
Views in ASP.NET MVC. It's our third tutorial in ASP .NET MVC series and in our first tutorial, we have seen How to
Setup Your First Project in ASP.NET MVC and in our second tutorial, we have seen
What is Model in ASP.NET MVC and I have told you about Views as well in previous tutorial. So, now we are gonna have a look at it in detail:
Views in ASP.NET MVC
- In MVC Framework, we don't have pages or path to some html file as in php.
- Instead, We have to use Views in ASP.NET MVC for front end designing.
- We can use Html, Css, Javascript, Jquery or any other front end language in Views.
- There are two types of Views, we use in ASP, which are:
- Specific Views ( These Views are associated with specific Controllers ).
- Generic Views ( These Views are associated with all the Controllers ).
- You can find the Views folder in your Solution Explorer, click to expand.
- In Views folder, you will find 4 more folders named as:
- Account.
- Home.
- Manage.
- Shared.
- In these folders, we have to click on Home to expand and you will find 3 files in it, which are:
- About.cshtml
- Contact.cshtml
- Index.cshtml
- If you remember thaat we also have same 3 pages on our demo ASP Web App.
- So, let's open Index file which is our home page View file and I have changed the H1 Tag from Asp.Net to The Engineering Projects, shown in below figure:
- Now let's refresh our web app, and you will get something as shown in below figure:
- Now you can see in above figure that our H1 Tag has now changed to The Engineering Projects.
- One important thing to note here is that we don't need to stop the visual studio server / execution because now we are making changes in the front end interface.
- When we were working on Models in MVC, we have to stop the Visual studio execution on every change because that was Server Side Programming.
- But as Views are Client Side Interface so we don't need to restart our server.
Specific Views in ASP.NET MVC
- As I have said earlier, there are two types of view and in this section we will first discuss the Specific Views in ASP.NET MVC.
- These Views are specific to certain Controllers, we will discuss Controllers in the next chapter.
- For now when a user enters the webpage, the controllers get the respective View and show it to the user.
- In the above section, we have seen the code for index file and you must have noticed that this index file only contains the html code for the body.
- This is a Specific View as all it's data is used only for Home Page, we can't use it our About or Contact Page.
- So, all the three Views in Home Folder are all Specific Views and are attached to specific Controllers.
- Now let's have a look at the Generic Views: ( Btw these are not their actual names, I have given these names to Views according to their functionality ).
Generic Views in ASP.NET MVC
- Let's have a look at the interface of our demo Web App:
- You can see in the above figure that we have a Menu Tab in our Home Page where we have placed the links of our 3 Pages.
- But in our Index View, we haven't seen any code for Menu Tabs.
- Now if you check all pages of your Web App then you will notice that they all have this same Menu Bar.
- Such Views are called Generic Views which are used in almost every page and decides the nature of your web design.
- Sidebar, Footer & Headers etc. comes in Generic Views category.
- So, now question is where we have the code for Menu Bar.
- Such Generic Views are placed in Shared Folder as shown in right side figure:
- If we have to apply some Generic Terms in Front End like Menus, Footer, Siderbars, Menu Layouts etc. then we will add their codes in _Layout.cshtml.
- All these other files are partial views, which will specifically render with specific controllers.
- We will discuss them in our coming tutorials but for now the file you should be interested in is _Layout.cshtml.
- So, open this Layout File and it will look like something as shown in below figure:
- Now you can see here, this file is in proper HTML Format having both Head & Body tags.
- This is our Main Layout View in which we can add all our generic styling items with proper placement.
- You have noticed a RenderBody() here and that's the place where our Index View file is appearing.
- So, when this View File is called then it checks which controller command is coming, if it's home then RenderBody will get Index View.
- If its About Controller then RenderBody will get the About View.
- How Controller is accessing these Views that we will check in our coming tutorial.
- Here's a video where one of our Team Member has explained Views in detail, it will give better understanding:
So, that was all about View in ASP.NET MVC. In my coming tutorial, I will give you a detailed overview of Controllers in ASP.NET MVC. So, take care and have fun !!! :)
What is a Model in ASP.NET MVC
Hello friends, I hope you all are doing great. Today, I am going to share the second tutorial in ASP.NET MVC series and I will explain the concept of Model in ASP.NET MVC. In our previous tutorial, we have seen How to
Setup Your First Project in ASP.NET MVC.
So, before developing our web app, we first have to discuss some concepts in MVC Framework and Model is one of them, which we will cover in today's tutorial. So, let's get started with it:
What is a Model in ASP.NET MVC ?
- In, ASP.NET MVC Framework structure, we have to understand 3 very simple blocks, which are:
- Model connects our web application with our SQL Database, it takes commands from the Controller and then get or set values in the Database and finally updates the View.
- We can say that Model is acting as a middle man between Controller, View & SQL Databases.
- If you are not understanding the Controller or View, that's no problem. We will cover them in next chapters, for now just focus on functioning of Model.
- So, in your Solution Explorer, you can see a folder named Models, click to expand and you will find 3 model files in it, as shown in below figure:
- You can see in above figure that Microsoft visual studio has auto created 3 Model files in our ASP web project.
- So, click AccountViewModels.cs file and it will open up as shown in below figure:
- Now you can see in above figure that our model file has some classes in it.
- If you check the whole file then you will find more than 5 classes in it.
- Each of these class is actually representing a unique model, all of these classes have certain rules attached to them.
- Now let's understand the working of our web app.
- When our ASP web application runs for the first time:
- The App goes to Controller.
- Controller hits the Model.
- Model gets or sets values into SQL Database & then return value to Controller.
- Controller returns value to View.
- I have shown this working in block diagram form here:
- Now in this model file, you have find for class RegisterViewModel, its the Model for our registration page, shown in below figure:
- The main thing to note in this class is that, we have created 3 variables with data types public string, named as:
- Email.
- Password.
- ConfirmPassword.
- The parameters assigned to these variables are get and set, and these variables will either Get value from the SQL Database or Set new value to it.
- So in simple words, the model is dealing with data in or out, it receives commands/data from controller and according to the rules set in each model, the data will be either stored in the SQL database or fetched from it.
- Model tells us, what kind of information is required and what kind of information is placed in the SQL Databases.
- The major code of MVC Framework is written in the Model section, because here we have to write all our rules and permissions to interact with the SQL databases.
- Here's a video where one of our team member has explained Model in ASP.NET MVC in detail:
So, that was all about today's tutorial on Model in ASP.NET MVC. I hope you have got the idea. In the next tutorial, we will have a look at What is View in ASP.NET MVC. Take care !!!