2

I have class which contains List:

public class Client
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<Address> Addresses { get; set; }
     }

How I can to pass the object of this type from View to Controller?

For example:

@Html.ActionLink("Edit", "Edit", item)

where item is an object of Client type. By fact, FirstName and LastName passed successful, but Addresses passed as NULL.

Thanks any help.

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

0

Did you new the list somewhere?

Strings are treated as value types so declaring them as a property will create the storage for them; but, the List is a reference type; so, you will need to allocate the memory for it or it will be null (as you are seeing).

markshancock
  • 690
  • 2
  • 7
  • 25
  • I tried to create the Client copy constructor, which contains initialized data such as var address = new Address {State = "NY", City = "New York"}; this.Addresses.Add(address); But Address List of the Client object is Empty. (I passed the Client to the Controller as @Html.ActionLink("Edit", "Edit", new Client(item))) – Inna Kharkovskaya Dec 05 '12 at 12:11