Model Binding, a simple, simple question
- by Paul Hatcherian
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 TrackChanges<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!