开发者

Model Binding Custom Type

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 e开发者_JAVA技巧xtend 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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜