Controller cannot get reference to subclass members

5 views
Skip to first unread message

Vinita

unread,
Aug 11, 2010, 3:48:06 PM8/11/10
to mvc2inaction-discuss
I have 2 classes:

public class CreditCard
{
public string CardNumber;
public string CardHolderName;
public string ExpiryDate;
}

public class ShippingDetails
{
public string Name { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public CreditCard CreditCard { get; set; }
}

I have a view which is bound to shippingDetails class like this:
Card Holder Name: <%= Html.EditorFor(x=> x.CreditCard.CardHolderName)
%>
Card Number: <%= Html.EditorFor(x => x.CreditCard.CardNumber)%>
Expiry Date: <%= Html.EditorFor(x=> x.CreditCard.ExpiryDate) %>

However when this view calls the Action method in the controller
shippingDetails.CreditCard is always null.

Can someone tell me why? Any help regarding this will be highly
appreciated.

Vinita


Jimmy Bogard

unread,
Aug 12, 2010, 10:35:06 PM8/12/10
to mvc2inacti...@googlegroups.com
The ModelBinder doesn't work with fields.  It has to be properties.

And I can't tell you how long it took me to figure this one out the first time :)



--
--------------------
Please feel free to comment on the progress of the book, the content, and the code samples.  All code and text is available on the book's GitHub site at http://github.com/jeffreypalermo/mvc2inaction.  We hope developing this book out in the open and with your feedback makes it a great book for the community.

Vinita

unread,
Aug 16, 2010, 1:22:56 PM8/16/10
to mvc2inaction-discuss
Thanks a lot Jim. Converting fields into properties worked.

On Aug 12, 9:35 pm, Jimmy Bogard <jimmy.bog...@gmail.com> wrote:
> The ModelBinder doesn't work with fields.  It has to be properties.
>
> And I can't tell you how long it took me to figure this one out the first
> time :)
>
> > athttp://github.com/jeffreypalermo/mvc2inaction.  We hope developing this
Reply all
Reply to author
Forward
0 new messages