Introduction of "Passing anonymous object to view in ASP.NET MVC"
In this article I am going to discuss about passing anonymous object to my view. In article - Dynamic view model ASP.NET MVC using ExpandoObject i elaborated method to pass dynamic view model using ExpandoObject option. So here in this article i will be reusing models and methods created in previous article.
Practical Approach for "Passing anonymous object to view in ASP.NET MVC"
Now lets dive into practical approach of passing anonymous object to view. As mentioned i will be reusing classes created in the previous article. i.e, Class" and "Student" -
As you can see in the above code snippet, I am trying to filter the list students based on Class ID. And my Index view is updated like below -
So here in the above code snippet i am trying to bind the students in Class ID = 1. Below is the screenshot of output from my application -
In this article I am going to discuss about passing anonymous object to my view. In article - Dynamic view model ASP.NET MVC using ExpandoObject i elaborated method to pass dynamic view model using ExpandoObject option. So here in this article i will be reusing models and methods created in previous article.
Practical Approach for "Passing anonymous object to view in ASP.NET MVC"
Now lets dive into practical approach of passing anonymous object to view. As mentioned i will be reusing classes created in the previous article. i.e, Class" and "Student" -
public class Class { public int ClassID { get; set; } public string ClassName { get; set; } } public class Student { public int StudentID { get; set; } public int ClassID { get; set; } public string StudentName { get; set; } }HomeController after updating looks like this -
public ActionResult Index() { Listclasses = new List () { new Class() {ClassID =1, ClassName = "Class1"}, new Class() {ClassID =2, ClassName = "Class2"}, new Class() {ClassID =3, ClassName = "Class3"} }; List students = new List () { new Student() {StudentID = 100, ClassID =1, StudentName = "Student1"}, new Student() {StudentID = 101, ClassID =1, StudentName = "Student2"}, new Student() {StudentID = 102, ClassID =1, StudentName = "Student3"}, new Student() {StudentID = 103, ClassID =2, StudentName = "Student4"}, new Student() {StudentID = 104, ClassID =2, StudentName = "Student5"}, new Student() {StudentID = 105, ClassID =3, StudentName = "Student6"} }; IEnumerable studentList1 = students.Where(s => s.ClassID == 1); return View(studentList1); }
As you can see in the above code snippet, I am trying to filter the list students based on Class ID. And my Index view is updated like below -
@model IEnumerable<dynamic> @{ ViewBag.Title = "Home Page"; } <p><b>Student Details</b></p> <table> <tr> <th>StudentID</th> <th>StudentName</th> </tr> @foreach (DynamicModelusingAnonymousobjects.Models.Student s in Model) { <tr> <td>@s.StudentID</td> <td>@s.StudentName</td> </tr> } </table>
So here in the above code snippet i am trying to bind the students in Class ID = 1. Below is the screenshot of output from my application -
Passing anonymous object to view in ASP.NET MVC |
Hope this article is helpful.
No comments:
Post a Comment
Comments Section