X

Subscribe To Our Mailing List

P.S I will never spam you...100% GUARANTEED!

Sunday, March 1, 2015

Dynamic view model ASP.NET MVC using ExpandoObject


Introduction of Dynamic view model ASP.NET MVC using ExpandoObject

In this "Dynamic view model ASP.NET MVC using ExpandoObject" article I am going to explain about passing dynamic model to our view in mvc. Here I am trying to use "ExpandoObjects". This has been introduced in .NET 4.0 version. An ExpandoObject object helps us to add or remove members dynamically at runtime.

Note: This article is just to give a information on how we can use dynamic models in mvc using ExpandoObject. But I am strictly against this approach as we can handle these stuffs with different approach as well (without using "ExpandoObject", i will discuss those approaches in the coming weeks though).

Now lets dive in to the practical approach of using dynamic view model using ExpandoObject.

Lets create new classes - "Class" and "Student" to demonstrate this -
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; }
}
So now lets update the existing Homecontroller to use "ExpandoObject". So below is the updated code -

public ActionResult Index()
{
 dynamic mymodelDetails = new ExpandoObject();
 
 List classes = 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"}
 };

 //mymodelDetails.Classes = new { Classes = classes }.ToExpando();
 //mymodelDetails.Students = new { Students = students }.ToExpando();

 mymodelDetails.Classes = classes;
 mymodelDetails.Students = students;

 return View(mymodelDetails);
}

So as you can see in the above code snippet, data are populated for these classes and two properties "Classes" and "Students" are added to my dynamic expandoobject - "mymodelDetails".

So these "ExpandoObject" properties are used in the existing Index.cshtml as below -

@model dynamic
@{
    ViewBag.Title = "Home Page";
}

Class Details
<table> <tr> <th>ClassID</th> <th>ClassName</th> </tr> @foreach (PassingDynamicModel.Models.Class c in Model.Classes) { <tr> <td>@c.ClassID</td> <td>@c.ClassName</td> </tr> } </table> <br/> <br />
Student Details
<table> <tr> <th>StudentID</th> <th>StudentName</th> </tr> @foreach (PassingDynamicModel.Models.Student s in Model.Students) { <tr> <td>@s.StudentID</td> <td>@s.StudentName</td> </tr> } </table>

After Executing this we get output like below -

Dynamic view model ASP.NET MVC using ExpandoObject

Hope this article is useful.



4 comments:

  1. You can also include Display Templates tutorial in this example itself and that would add to your explanation of ExpandoObjects (suggestion)

    ReplyDelete
  2. Good suggestion. Appreciated !! I will try to cover that in my coming posts..

    ReplyDelete
  3. What about sending. Synamic object from a view to action method??

    ReplyDelete
    Replies
    1. If you want to post the data then use either TextBox or Hidden or Editor controls like below -

      @HTML.TextBoxFor(c => c.ClassID)
      @HTML.TextBoxFor(c => c.ClassName)

      and in post method you can use "FormCollaection" to get these values.

      Delete

Comments Section