This article will tell you almost everything about passing the data from Controller to View in ASP.NET MVC.
I am writing this article to tell you the basic to advanced foremost concepts about ways to pass the data from Controller to View. This article is the first one in the series named “Passing Data from Controller to View”. You’ll learn all the ways in each separate article. Stay tuned!
I would strongly recommend reading my previous articles, which will be defining the basic concepts of ASP.NET MVC.
- Action Selectors In ASP.NET MVC
- Filters In ASP.NET MVC
- Action Result In ASP.NET MVC
- MVC Architecture And Its Pipeline
- Convention Routing VS Attribute Routing
The topics to be covered are,
- Types of Data
- Passing the data from Controller to View
As we know Controller is the class whose responsibility is to handle all the HTTP requests and then execute an appropriate View. A View gives us the HTML markup that we display to the user. And for displaying anything on the View, there should be some data that is passed from Controller to View. And in this and next articles, you will learn how to pass the data from a Controller to View.
There are a number of ways to pass the data from Controller to View and also, the types of data that exist. Let’s move towards its types and then see the practical way to pass the data from Controller to View.
Types of Data
There can be two types of data to be passed from Controller to View.
- Weakly typed data
- Strongly typed data
Passing the strongly typed data from Controller to View
To pass the strongly typed data from Controller to View, we have to make a model class then populate its properties with some data and then pass that data to the strongly typed view. Let’s create a project to take a look at this example.
Go to File >> New and select the “Project” option.
Now, create a class named “Employee” in the Models folder, as shown below.
- namespace DataCtoV.Models
- {
- public class Employee
- {
- public byte EmployeeId { get; set; }
- public string EmployeeName { get; set; }
- public string Address { get; set; }
- public string Phone { get; set; }
- }
- }
- public class EmployeeDataController : Controller
- {
- // GET: EmployeeData
- public ActionResult GetEmployeeData()
- {
- List<Employee> emp = new List<Employee>
- {
- new Employee
- {
- EmployeeId = 1,
- EmployeeName = "John",
- Address = "12 Fremont St. Clermont, FL 2813",
- Phone = "+1-234-2838421"
- },
- new Employee
- {
- EmployeeId = 2,
- EmployeeName = "Smith",
- Address = "14 Highland Drive Fort Worth, TX 3994",
- Phone = "+1-234-2244521"
- },
- new Employee
- {
- EmployeeId = 3,
- EmployeeName = "Marry",
- Address = "23 Fremont Road Milledgeville, GA 6788",
- Phone = "+1-234-46568421"
- }
- };
- return View(emp);
- }
- }
- @model IEnumerable<DataCtoV.Models.Employee>
- @{
- ViewBag.Title = "Employee Data";
- }
- <h3>Employee Data</h3>
- <table class="table table-condensed table-hover">
- <thead>
- <tr>
- <th>@Html.DisplayNameFor(e => e.EmployeeId)</th>
- <th>@Html.DisplayNameFor(e => e.EmployeeName)</th>
- <th>@Html.DisplayNameFor(e => e.Address)</th>
- <th>@Html.DisplayNameFor(e => e.Phone)</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var employee in Model)
- {
- <tr>
- <td>@employee.EmployeeId</td>
- <td>@employee.EmployeeName</td>
- <td>@employee.Address</td>
- <td>@employee.Phone</td>
- </tr>
- }
- </tbody>
- </table>
Because we are passing the strongly typed data from Controller to View, we have to create a strongly typed View by using the following line of code.
- @model IEnumerable<DataCtoV.Models.Employee>
Now, let’s build and run the application. You should see the following output.
To change this name, we have to apply a data annotation to EmployeeId and EmployeeName property, named as Display and then set its Name property to “Serial No” and “Name” respectively. You can see the changed code of “Employee” model class below.
- [Display(Name = "Serial No")]
- public byte EmployeeId { get; set; }
- [Display(Name = "Name")]
- public string EmployeeName { get; set; }
In this article, you have learned how to pass strongly typed data from Controller to View, in which firstly, you should make a model class then populate its properties with some data then pass that data to the strongly typed View. And a strongly typed View can be made by using @model declaration in the View file having .cshtml extension. You have learned it with the help of an example.
Conclusion
I hope this article has helped you in understanding the concepts about passing strongly typed data from Controller to View in ASP.NET MVC. Stay tuned for my next articles because this article is the number one in the series of “Passing data from Controller to View” and you’ll learn more about passing the data in coming articles. If you have any query, please feel free to contact me in the Comments section. Also, do provide the feedback whether positive or negative. It will help me to make my articles better and increase my enthusiasm to share my knowledge.
No comments:
Post a Comment