Skip to main content

ASP.NET MVC 5 (Part 2)

Before you start this tutorial, you should have the knowledge about, how to create an ASP.NET MVC  web application.

MVC stands for the model- View- controller.MVC is an architectural pattern for developing applications that are well architectured, testable and easy to maintain.MVC application contains:

Model: classes represent the data of the application. A model does not depend on the controller and view classes.
View: Display the model data and send user action (button click) to the controller.
Controller: provides the model data to the view and interprets user actions like button click. the controller depends on the model and the view.

Controller

Let 's start by adding a controller class in MVCProject.In Solution Explorer, right click on the controller folder and select controller.

 In the Add scaffold dialogue box,  select MVC 5 controller-empty and then click Add.
Name your controller FirstTest, and click the Add button.
Notice that in Solution Explorer, a file FirstTestController has been generated and a folder name FirstTest has generated at the path Views/FirstTest.
    In the controller class, add the following two methods.

 //
        // GET: /FistTest/

        public string Index()
        {
            return "This is my <b>default</b> action...";
        }

        //
        // GET: /FistTest/Welcome/

        public string Welcome()
        {
            return "This is the Welcome action method...";
        }

The controller is named FirstTest and a first method named is Index.both controller methods will return a string as an example. Run the application, in the browser, append "FirstTest" with the URL (For example in the below illustration I appended "FirstTest in the address bar"). The page in the address bar will look like the following screenshot. Above method in the controller will return the only string.
The default URL routing logic used by ASP.NET MVC uses a format like this to determine what code to invoke.
/[Controller]/[ActionName]/[Parameters]
We can set the routing format in the App_Start/RouteConfig.cs file


when we run the application and don't append any segment in the address bar.it default to the "Home" controller and then "index" action method as specified in the above code. 

The first part of the URL is controller like \FirstTestController.The second part is the action method index like /FirstTestController/Index. In the above example, we had only append FirstTest controller class and it called by default index method. the third part of URL is parameters which are for route data.we will discuss later in this tutorial.

Let's call the second method of FirstTestController class. Run the application again and append FirstTest/Welcome.this will return the following string.
Let's modify the example slightly so that you can pass some parameter information from the URL to the controller (for example, /FirstTest/Welcome?name=Sarah&numtimes=3). Change your Welcome method to include two parameters as shown below. Note that the code uses the C# optional-parameter feature to indicate that the numTimes parameter should default to 1 if no value is passed for that parameter.

public string Welcome(string name, int numTimes = 1)
        {
            return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
        }

Run the application and browse to the example URL (http://localhost:xxxx/FirstTest/Welcome?name=Sarah&numtimes=3). We can try different values for name and numtimes in the URL. The ASP.NET MVC model binding system automatically maps the named parameters from the query string in the address bar to parameters in your method.


In these examples, the controller has been doing the "VC" portion of MVC — that is, the view and controller work. The controller is returning HTML directly.

Now you have the knowledge about the how controller classes work using routing configurations. In another article, we will get the knowledge about the "View" part of the MVC.

Happy Programming :) 

Comments

Popular posts from this blog

Introduction of Arrays

An array is a linear data structure which stores collection of data in a contagious memory location. The idea is to store the collection of the same type of data. this makes it easier to calculate the position of each item by simply adding an offset to a base value. In the above image of an array, we can identify each element by its index. we can declare an array by specifying the types of its element. type [] arrayname; Types of Arrays single dimensional arrays Multidimensional arrays Single Dimensional Arrays A single dimensional array can be declared in the following way. int [] array = new int [ 5 ]; this array will contain the element from array[0] to array[4].the new operator will initialize  each  element of this array with zero. An array which contains string value can be declared the same way. string[] array = new string[6]; Array Initialization we can also initialize an array while declaration. For example int

How to upload a file on the server through upload control in asp.net

With ASP.NET upload a file on the server is very easy.With FileUpload control we can easily upload a file on the server.Following markup is required.   < form id ="form1" runat ="server" > < asp:FileUpload id ="FileUploadControl" runat ="server" /> < asp:Button runat ="server" id ="btnupload" text ="Upload" onclick =" btnupload _Click" /> < asp:Label runat ="server" id ="lblstatus" /> </ form > Here is the CodeBehind code required to handle upload a file on the server. protected void btnupload _Click( object sender, EventArgs e) { if (FileUploadControl.HasFile) { try { string filename = Path.GetFileName(FileUploadControl.FileName); FileUploadControl.SaveAs(Server.MapPath( " ~/ " ) + filename); lblstatus .Text = " Upload status: Fi

ASP.NET MVC 5 (Part 1)

This tutorial teaches you the basics of building an ASP.NET MVC 5 web app using  Visual Studio 2017 . Get started Start by installing Visual Studio 2017. You can  Install Visual Studio 2017 community version which is free. Open Visual Studio,  Instead of selecting  New Project  on the  Start page , you can use the menu bar and select  File  >  New Project . Your first app On the  Start page , select  New Project . In the  New Project  dialog box, select the  Visual C#  category on the left, then  Web , and then select the  ASP.NET Web Application (.NET Framework)  project template. Name your project "MVCProject" and then choose  OK . In the  New ASP.NET Web Application  dialog, choose  MVC  and then choose  OK . The visual studio used the default template for the project you just created.you have a running application which is doing nothing right now. Press  F5  to start debugging. When you press  F5 , Visual Studio starts  IIS Ex