Custom model binding type

I have a structure that is very similar to 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 am trying to use it with ASP.NET MVC binding. Using the default client model binding, the property will always be null. I can fix this by adding ".Value" to the end of each form input name, but I just want it to bind to the new type directly using some kind of binding device, but all the solutions I tried seemed unnecessarily complicated. I feel like I should be able to extend the default binder and, with a few lines of code, redirect property binding to the entire model using an implicit conversion. I don't quite understand the default binder binding paradigm, but it seems like she's really stuck with this distinction between model and model properties. What's the easiest way to do this? Thanks!

+2


a source to share


1 answer


Found the answer. I just needed to implement a custom type converter and the default linker was added by default.



+3


a source







All Articles