Asp.net MVC Basics

Datetime:2016-08-23 04:40:25          Topic: ASP.NET MVC           Share

This article written for the purpose of gives you some basic key point of MVC. I read many article over internet on MVC technology and think to summarize all articles key points in one article. It’s not possible to publish all the key points in one article so think to publish remaining key points in my next articles.

In this articles I will cover the topic likes Razor Engine, Layout Page, Partial Page, Model in MVC, Scaffolding In MVC .

Razor Engine :

1) Razor Engine comes to the picture due to make the clean syntax while writing code.

2) Razor Engine recognize the code if the code starts with the symbol ‘@’.

3) Razor is more smart enough that while we use ‘@’ symbol then it immediately recognize the code and stop if he got any blank space.

4) If we writing single line code using Razor syntax then to separate the code form the html content we use the open parenthesis symbol.

For eg:- @string message=”Hello”,

@message.In

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:-

@(message).in

5) Razor makes the code much more neat and clean so that it is easy to write any server side code inside the view.

6) Razor within it contain HTML Encoding mechanism; due to this reason while we write anything using razor symbol then it automatically convert to html.

For eg:- If I write @string message=”Hello”

@message

o/p: Html content in page “ span Hello span” like that.

7) In Razor if we want that razor don’t 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=”Hello”;

@Html.Raw(message)

Then

Razor

won’t Encode this message to HTML rather then it print same as it is “Hello”.

Layout Page in MVC:

1) Often we see that we required same layout in 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.

2) 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.

3) 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:

1) 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()”.

2) 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.

3) 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 term 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 models in our project. One of the best use of model class is used at the time of Scaffolding.

SCAFFOLDING In MVC:

Scaffolding in MVC crated 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.

1) Scaffolding is the best option to create view without writing code; but it does not mean that scaffolding will create complete application.

2) Another thing is that if you don’ t like scaffolding then you go form 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 type of design partern technique to build the scaffolding .So that your choice whether you use scaffolding or not or else you go form scratch.

Importance of form tag:

  • Form Tag is Powerful In the internet the most-used tag is form tag; without form tags the internet would be a read only repository of boring documentation. You wouldn’t even be able to search for anything on the internet without a form tag. So it plays an important role in the web for sending and receiving data.
  • Before ASP.NET MVC Before it came into the picture we had ASP.NET webforms and many developers who came directly to MVC didn’t know about ASP.NET web forms. ASP.NET web forms don’t expose the full power of form tags, which means you could say it’s used as its own requirement.

Apart from that, as we all know, form tag is a container which contains buttons, checkboxes, and more. In Html form it enables us to enter information and submit it to the server.

But which server it going to post the data ?

The answer to the above question is the two most important attributes of form tags, and these are:

The action and the method attribute

.

The action attribute tells the web browser where to send the information (or on which server to post the data). The action attribute naturally contains the URL.The URL may be absolute or relative.

For example, in the Bing search engine the code of the form tag is shown below,

form action="http://www.bing.com/search"
input name="s" type="text"
input type="submit" value="Search!"
form

In the above code you might think that form tag does not contain any method attribute, so then how it is going to know what type of method attribute to use?

To understand this we need to understand some important concepts about form tag:

  • The method attribute inside form tag tells the browser whether to use Http Get or Http Post while sending data to web server. You might think that by default the method is Http Post; but that is wrong. Whatever you are doing, whether submitting your profile image or buying something from internet, the default method used inside the form is Http Get.
  • So remember, while posting any data to server, the default method is Http get; often we confuse this question in interviews regarding what default method is used for form tag and we think that we are posting data for form tag, so default will be post, but it doesn’t work like that. You are posting data, yes it’s true, but after posting does the browser get some data? The answer is “yes,” so the default is “Get.”

Important Concepts About Get and Post :

  • Both Get and Post are action attribute which is generally used for sending the data to server, many time we confuse that “Get” attribute is used for getting data from server.

But not like that both of the attribute only post the data to server.

  • Difference between Get and Post attribute :- When we used “Get” attribute and posting data to server at that time the data goes to server by Query string with in URL.

Where as when we use “Post” attribute data goes to the server by inside the body .

Second Difference when we use “Get” attribute it did not create any effect to server; that means it’s did not change the state the server; and won’t create any problem if we make the request frequently.

Where as if we use “Post” it’s change the state of the server and if we make the request frequently then it create problem and the browser show the alert message.

So while we do any type of transaction at that time the action attribute use as “Post”, but in Search engine basically used the action method as “Get”

  • Web application generally used Get attribute to read the data when we required to change the data on sever (i.e CRUD Operation) then we use the attribute as “POST”.

For eg:- Searching an item for this we need to use “Get”,where as if we want to “Write” some data inside the document or we want to update the data the the action attribute we used as Post.

Thanks for reading; if you like this articles share it.Don’t forget to give your comment.

Happy Coding.





About List