This article is written for the purpose of giving you some basic key points of MVC. I read many articles over the internet on MVC technology and thought to summarize all articles' key points in one article. It’s not possible to publish all the key points in one article so I thought to publish remaining key points in my next articles.
In this article I will cover topics likes Razor Engine, Layout Page, Partial Page, Model in MVC, Scaffolding In MVC.
Razor Engine :
- Razor Engine comes to the picture to make clean syntax while writing code.
- Razor Engine recognizes the code if the code starts with the symbol ‘@’.
- Razor is smart enough that while we use ‘@’ symbol then it immediately recognizes the code and stops if it gets any blank spaces.
- If we are writing a single line of code using Razor syntax then to separate the code form the html content we use the open parenthesis symbol.
For eg
- @string message=”Hello”,
- <lable> @message.In</lable>
Hear we can get the error immediately once we type ‘.’ So to do this we need to put parenthesis after the razor syntax as follows,
- <lable>@(message).in</lable>
- Razor makes the code much more neat and clean so that it is easy to write any server side code inside the view.
- Razor within it contains HTML Encoding mechanism; due to this reason while we write anything using razor symbol then it automatically converts to html.
For eg - If I write,
- @string message=”<span>Hello</span>”
- <lable>@message</lable>
o/p: Html content in page “ <gt>span<lt> Hello <gt>span<lt>” like that.
- In Razor if we want razor not to Encode the Html automatically then we need to create the instance of “system.web.IHTML” or we can create the instance of “Html string” or we can pass that variable inside the HTML.Raw() method.
For eg - If we print the above scenario then we need to do by the following way,
- @string message=”<span>Hello</span>”;
- <lable>@Html.Raw(message)</lable>
Then Razor won’t Encode this message to HTML rather then it print same as it is “<span>Hello</lable>”.
Layout Page in MVC
- Often we see that we require the same layout in a different page; so to use the same layout in different page we use the concept of Layout page and Layout page is same as the Master page used in Web forms of ASP.net.
- In Layout page we have different method like Renderbody(), RenderSection() and what ever method we defined inside the layout page it’s mandatory that we need to pass the value for this method.
- So It’s necessary that any method we defined in Layout page then we required to pass the particular section; but their is a twist is that we can make that section as optional as well by setting “required” attribute as false.
For eg:- RenderSection(“Hello”,false)
So here it’s not mandatory that we required to render that section it’s now become optional.
Partial View In MVC
- Partial view is same as normal view page but the difference is that to render partial view we need to call the “render Partialview()” method where as for view simply “return view()”.
- Another big difference is that if any layout page specified inside “_ViewStart.cshtml” page then partial view not use that layout page, if the layout defined inside the partial page then only the layout page going to render.
- When to Use Partial Page :-When we requirement that to call a page without having any layout just to render particular content then we go for the Partial page option. For eg:- We have different view inside a page then we have use Partial page.
MODEL In MVC:
Model in Simple terms represent as Structure. So in technical manner model is nothing but a class which has some set of property. This model object used for send information to database perform business calculation, and even render view . In other word, these objects represents the domain the application focus on, and the models are the object you want to display, save ,create, update and delete.
After creation of the model or classes the next step is to understand how to use this model in our project. One of the best uses of model class is used at the time of Scaffolding.
SCAFFOLDING In MVC:
Scaffolding in MVC creates a view controller for you without writing single line of code and it’s so much smart enough that he know which view to present which folder. So through scaffolding we can create different type of view.
- Scaffolding is the best option to create view without writing code; but it does not mean that scaffolding will create complete application.
- Another thing is that if you don’ t like scaffolding then you go from scratch and design your own page. Don’t think that the number of scaffolding is only four, we have different scaffolding on Nuget package manager where you find number of scaffolding which uses different types of design pattern techniques to build the scaffolding so that your choice whether you use scaffolding or not or else you go from scratch.
Thanks for reading; if you like this articles share it. Next article comes with Key Point of Entity Framework in MVC.