I created a custom string object, but it does not modelbind when I post it back to the server. Is there an attribute I’m missing on the class or something?
This is the custom string class below:
public class EMailAddress
{
private string _address;
public EMailAddress(string address)
{
_address = address;
}
public static implicit operator EMailAddress(string address)
{
if (address == null)
return null;
return new EMailAddress(address);
}
}
In order for the object to be correctly bound by the default model binder it must have a default parameterless constructor:
if you want to use models as the one you showed you will need to write a custom model binder to handle the conversion:
which will be registered in
Application_Start:and used like this:
Now when you query
/Home/Index?email=foo@bar.bazthe action parameter should be properly bound.Now the question is: do you really want to write all this code when you can have a view model as the one I showed initially?