Controllers in MVC
In an MVC application, every Controller is created as a class. A project can have more than one controllers according to our requirement.
Important points to create the Controller
- Controller class should be declared with Public Access Modifier.
- Controller class name should end with the Controller prefix.
- All the Controllers should be inherited from the Controller base class which provides the build in Objects, Methods etc.
In Controller, at the time of processing a request, it will execute the corresponding method which is called Action Method.
Action Method in MVC
Important points about Action Methods
- Action method must be Public.
- It can’t be static, it must be a non-static method.
- Action method returns the View() method. View() is used to process the Views. The View() method is a built-in method in the controller class, at execution time view() execute and result will return from the action method. View() method return type is View Result.
- Action method can be parameterized or parameterless.
- It can overload but can’t be overridden.
An action method can be defined with various return types
Type of Action Method
- Action method with view result: These methods return View Page as a result
- Action method with the non-view result: Action method that returns data instead of an HTML Page.
- No action Method
- ViewResult
- Partialviewrestult
- FileResult
- JsonResult
- ContentResult
- JavascriptResult
- RedirectResult
- EmptyResult
ViewResult
PartialviewResult
JsonResult
RedirectResult
RedirectToRouteResult
ContentResult
JavascriptResult
EmptyResult
Data Sharing between controller to view
- ViewBag
- ViewData
- TempData
- Strongly Typed View
ViewData
- Key-string
- Value-object
ViewBag
Syntax
- ViewBag.key=value
- var a =viewBag.key
TempData
Syntax
- TempData["Key"]=value;
- var a =(type)TempData["Key"];
TempData is an object of TempDatadictionary class.
- public ActionResult DatashaingExample()
- {
- //ViewData Example
- List<string> cities = new List<string>()
- {
- "Jaipur",
- "Delhi",
- "Ajmer",
- "Noida"
- };
- //ViewBag Example
- ViewData["Cities"] = cities;
- List<string> states = new List<string>()
- {
- "Rajasthan",
- "UP",
- "Mp",
- "Punjab"
- };
- ViewBag.state = states;
- //TempData Example
- List<string> countries = new List<string>()
- {
- "India",
- "USA",
- "UK",
- };
- TempData["Counties"] = countries;
- return View();
- }
- @{
- ViewBag.Title = "Data Sharing Example";
- }
- <h3 class="btn-primary" style="text-align:center">ViewData Example</h3>
- <ul>
- @foreach(var city in (List<string>)ViewData["Cities"])
- {
- <li>@city</li>
- }
- </ul>
- <h3 class="btn-primary" style="text-align:center">ViewBag Example</h3>
- <ul>
- @foreach (var state in @ViewBag.state)
- {
- <li>@state</li>
- }
- </ul>
- <h3 class="btn-primary" style="text-align:center">TempData Example</h3>
- <ul>
- @foreach (var country in (List<string>)TempData["Counties"])
- {
- <li>@country</li>
- }
- </ul>
Run the project and check result.
Strongly typed views
Advantages of using Strongly-typed View
- It supports intelligence
- Not need to perform type casting
- Make database related operations easy.
- public class Employeedetails
- {
- public int Id { get; set; }
- public string EmployeeName { get; set; }
- public string City { get; set; }
- public string Departmnet { get; set; }
- }
- public ActionResult EmpDetails()
- {
- List<Employeedetails> ED = new List<Employeedetails>();
- Employeedetails ED1 = new Employeedetails()
- {
- Id = 1,
- EmployeeName = "Sanwar",
- City = "Jaipur",
- Departmnet = "IT"
- };
- Employeedetails ED2 = new Employeedetails()
- {
- Id = 2,
- EmployeeName = "John",
- City = "Jaipur",
- Departmnet = "IT"
- };
- ED.Add(ED1);
- ED.Add(ED2);
- return View(ED);
- }
- View Name: EmpDetails
- Template: Empty
- Model class: Employeedetails
- @using MvcPartOne.Models;
- @model List<Employeedetails>
- @{
- ViewBag.Title = "EmpDetails";
- }
- <h2 class="btn-primary" style="text-align:center">Employee Details</h2>
- <table class="table table-responsive">
- <tr>
- <th>Id</th>
- <th>EmployeeName</th>
- <th>City</th>
- <th>Department</th>
- </tr>
- @foreach (Employeedetails item in Model)
- {
- <tr>
- <td>
- @item.Id
- </td>
- <td>
- @item.EmployeeName
- </td>
- <td>
- @item.City
- </td>
- <td>
- @item.Departmnet
- </td>
- </tr>
- }
- </table>

Abhishek MishraPosted Apr 16, 2019, 1:32 AM
Good quick refresher on ASP.NET MVC