Saturday, November 3, 2018

Asp.Net MVC Views

Asp.Net MVC Views are stored in views folder with the same name of the controller in controller folder. Every View derived from WebViewPage Class in System.Web.Mvc namespace. Asp.net MVC supports the Razor View Engine with the extension of .cshtml. Views are used to display the data from the model with help of controller. The controller takes the income HTTP request and renders the specified View with data to the browser.

Asp.Net MVC Views


How to create Views in Asp.Net Mvc. Open Visual Studio click on File --> New --> Projects. and give the name as alltechgeeks.


Asp.Net MVC Views all tech geeks

Next, select the Empty option from top pan and click MVC checkbox, and click ok Button.

Asp.Net MVC Views all tech geeks

You get the application structure and as shown below image and right click on controller add controller name.
Asp.Net MVC Views all tech geeks
with views in views folder and no classes in controllers
                                           
                                           
Asp.Net MVC Views all tech geeks
Right click on Controller

Asp.Net MVC Views all tech geeks
Give the name to the controller

Finally, the controller is created and also a view folder in created in the view folder as sown below.

Asp.Net MVC Views all tech geeks

Now Add the view for controller right-click on action result Index. give the name same as action result name to view also.
 asp.net MVC view

Now .chhtml file will be generated and run the .cshtml file find out pt in the browser.

 asp.net MVC view
Index.cshtml file
 asp.net MVC view
This is the output asp.net MVC view

Asp.net MVC uses the Razor view engine and generates the .cshtml files. Razor view means combining the Server side code (C# code) with HTML code. here we are using the @ character at server side code. in asp.net we are using <% %>. but in asp.net MVC @ character reduces the time to develop the application.
 
Example code for Razor

@{
    ViewBag.Title = "Index";
    }

<h2>Welcome to my first All Tech Geeks Views Part</h2>











No comments: