Reputation: 11155
I have this controller:
public JsonResult Register(string step , GlobalOnBoardingDataModel data){}
the MVC wont deserialize the data
variable.
The step
variable is getting the post var into it.
I have other classes that are been deserialized fine, but this one won't. this is the class:
public class GlobalOnBoardingDataModel : BaseStepDataModel
{
public string Email = string.Empty;
public string FirstName = string.Empty;
public string LastName = string.Empty;
public string PhoneAreaCode = string.Empty;
public string PhoneCountryCode = string.Empty;
public string PhoneNumber = string.Empty;
public string CountryShortName = string.Empty;
public string AccountCurrency = string.Empty;
public float InitialDepositAmount = 0;
public string Address = string.Empty;
public string City = string.Empty;
public string State = string.Empty;
public string DateOfBirth = string.Empty;
public string CreditCardNumber = string.Empty;
public string CreditCardExperationDate = string.Empty;
public bool RiskDisclosure = false;
public bool Beneficiary = false;
public string CVVNumber = string.Empty;
public bool Subscribe = false;
}
BTW, The location of the var is not an issue, seccond place working with other cases.
Edit these are the values (from chrome):
step:one
FirstName:kjh
LastName:
CountryShortName:IL
PhoneNumber:
PhoneAreaCode:
PhoneCountryCode:
Email:[email protected]
Thanks
Upvotes: 0
Views: 469
Reputation: 42333
Assuming the post body is correct; you need to rewrite the fields and turn them in to public get/set
properties.
MVC won't model bind to fields.
Since you wish to have non-default defaults for your members; you can use a default ctor to set the property defaults on construction; although I would prefer the approach that doesn't use auto-implemented properties instead:
private string _email = string.Empty;
public string Email { get { return _email; } set { _email = value } }
In reality, I suppose there's little difference at runtime - but if you use the constructor approach it's very easy to forget adding a new default to it when adding a new property.
MVC won't bind to fields ultimately because it uses the System.ComponentModel.TypeDescriptor
class, in a round-about way, to get the metadata used to identify model properties that are to be bound. This isn't a bug, though, it's by design and is a good design choice as it enables mechanisms other than reflection the ability to be used in describing model types.
Upvotes: 2