2

I have the following class definition:

public class CallGroupViewModel
    {
        public string Name { get; set; }
        public string BulkEmails { get; set; }
        public List<dynamic> Members {get;set;}

    }

The Members property is some dynamic knockout code and the contents change depending on UI stuff. I am wanting to take a short cut since the UI is so up in the air right now.

My problem is that when it get to my action method, everything gets populate, including Members but its a array of objects and I can't get at the individual properties on them.

public ActionResult SaveGroup(CallGroupViewModel group)

group.Members //this has a count
group.Members[0].email //this pukes as 'email' is not valid for 'object'

I'm probably just missing something, any help is appreciated.

Thanks!

Matt
  • 3,638
  • 2
  • 26
  • 33
  • What are you setting `Members` with? Does an object in Members have a property `email`(case sensitive of course). – Srikanth Venugopalan Mar 12 '13 at 03:01
  • Its done via KnockoutJS on the client. Essentially this: {email:"",id:"",name:""} – Matt Mar 12 '13 at 03:02
  • So something like this `group.Members.add(new {email ="",id ="",name=""})`? I understand that you want to bind this as Javascript object to KO, but I am trying to understand the C# part. – Srikanth Venugopalan Mar 12 '13 at 03:04

2 Answers2

0

For accessing dynamic properties from the object, in your case you can use :

var email = group.Members[0].GetType()
                            .GetProperty("Email")
                            .GetValue(group.Members[0], null); 
Gaurav
  • 8,367
  • 14
  • 55
  • 90
0

The model binding system which populates MVC action method arguments isn't intended to work with dynamic objects. The system requires type information to know which information to bind. Consider changing your model to use strong types instead.

Levi
  • 32,628
  • 3
  • 87
  • 88