Asp.net MVC: Understand Controller and view concepts
Asp.net is comprised of 3 sections i.e. Model,View,Controller. In Asp.net MVC when a user clicks on a link request follows the route given below
- Request come to the controller
- Second, Depending on the action, controller makes the object of the model. Then model calls the DAL (Data Access Layer) that fetches data.
- Fetched data is then pass to view for display.
What is controller?
In asp.net web forms code behind work is replaced by controllers in Asp.net MVC. Controllers perform the same function as code behind does i.e. to write the logic of the program.Let build a project and try to understand the concept of controller with code
Open Visual Studio -> File -> New -> Project -> Click web -> Choose ASP.NET Web Application and click OK.
In solution explorer ,Right click Controller folder and add a Controller with the name of Test. Assume we have to display the values of Customer First Name , Customer Last Name , Customer Address. Firstly, open the new created class TestContoller in the folder of Controller. You will find an action method there.Remove this action method and add a Public class with the name of Customer and write the get and set properties. After creating class in the TestController, add a method with the name of GetCustomer. The code is given below
namespace AspMVC.Controllers
{
public class Customer
{ public string FirstName{get; set;}
public string LasttName{get; set;}
public string Address{get; set;}
}
public class TestController : Controller
{
public Customer GetCustomer()
{
Customer c = new Customer();
c.FirstName = "Ali";
c.LasttName = "ahmed";
c.Address = "276 Lahore";
return c;
}
}
}
Press F5 . Open the browser and write"ContollerName/ActionName" in the address bar. In our case it would be Test/GetCustomer. Note that you have to only write controller name i.e. Test not TestController. We will get AspMVC.Controllers.Customer .
Note that we have written return type "Customer" while writing method GetCustomer().So, in this case , when return type is some object like "Customer" .It will return "ToString()" implementation of that object i.e. NameSpace.ClassName. To get values of properties we have to override "ToString()" method.Put the following code in public class Customer.
public override string ToString()
{
return this.FirstName + "," + this.LasttName + "," + this.Address;
}
You will get desired output i.e. Ali,ahmed,276 Lahore.
What is view?
As we have discussed above that controller will handle the user request and display the response . Commonly the response is often HTML as browser can better understand it.Defining the user interface design termed as UI layer while in Asp.net we define it as view.To understand the concept of view we do some practice add a method with the name of GetView in Test controller given below
public ActionResult GetView()
{
return View("MyView");
}
Right click on Getview() . A new dialog box will open write the name of view as "MyView" and click add. Now open the view folder in solution explorer you will find there Test sub folder open it .There will be MyView.cshtml page you have created. Now add content in view such as
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyView</title>
</head>
<body>
<div>
My first Application
</div>
</body>
</html>
Press F5 and Write the same in address bar as ControllerName/ActionName i.e. Test/GetView. Remember do not write Test/MyView . Output will be
My first Application
In Asp.net MVC views associated with a particular controller will be placed in its folder with the name of that specified controller i.e. Test in our case and these views will always be available to only that controller . If other controllers want to access these view then you have to save these views in shared folder. Views and controllers are not tightly coupled . One action method can refer to many views and one view can referred by more than one action method . So working with Asp.net MVC , We can reuse code in a better way.