views:

36

answers:

3

I have the following class:

public class PostCode {
    public string Name { get; set; }

    public static implicit operator PostCode(string postCode)
    {
        return new PostCode {Name = postCode};
    }
}

It forms part of an Address class which is the model for a helper template (EditorTemplates>Address.ascx).

This helper template is rendered using <%= Html.EditorFor(model => model.Address)%> where Address is the property on another object.

Everything in the address is correctly bound when posting to the action method apart from the PostCode object. It seems likely that this is due to the fact that it is stored as a PostCode instead of a string.

How can I force the model binder to honour this cast?

A: 

Hi,

Did you have EditorTemplate for PostCode? If not create it.

muek
A: 

This seems similar to a problem I had.

See this http://stackoverflow.com/questions/1471257/net-mvc-custom-types-in-urls

You will have to implement your own model binder to do the job.

Gnomo
A: 

I ended up using a string to represent the postcode in the ViewModel and did the conversion in the mapping to my domain entity.

David Neale