Question

I have a struct which works much like the System.Nullable type:

public struct SpecialProperty<T>
{
    public static implicit operator T(SpecialProperty<T> value)
    {
        return value.Value;
    }
    public static implicit operator SpecialProperty<T>(T value)
    {
        return new SpecialProperty<T> { Value = value };
    }

    T internalValue;
    public T Value { get { return internalValue; } set { internalValue = value; } }

    public override bool Equals(object other)
    {
        return Value.Equals(other);
    }
    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }
    public override string ToString()
    {
        return Value.ToString();
    }

}

I'm trying to use it with ASP.NET MVC binding. Using the default customer model binder the property will always yield null. I can fix this by adding ".Value" to the end of every form input name, but I just want it to bind to the new type directly using some sort of custom model binder, but all the solutions I've tried seemed needlessly complex. I feel like I should be able to extend the default binder and with a few lines of code redirect the property binding to the entire model using implicit conversion. I don't quite get the binding paradigm of the default binder, but it seems really stuck on this distinction between the model and model properties. What is the simplest method to do this? Thanks!

Was it helpful?

Solution

Found the answer. I just needed to implement a custom type converter and the default model binder picked it up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top