我有以下类(class):
public class PostCode {
public string Name { get; set; }
public static implicit operator PostCode(string postCode)
{
return new PostCode {Name = postCode};
}
}
它构成了 Address
的一部分类,它是辅助模板的模型 (EditorTemplates>Address.ascx)。
此辅助模板使用 <%= Html.EditorFor(model => model.Address)%>
呈现其中 Address
是另一个对象的属性。
除了 PostCode
之外,在发送到操作方法时,地址中的所有内容都已正确绑定(bind)目的。这似乎是因为它存储为 PostCode
。而不是字符串。
我如何强制模型绑定(bind)器接受此类型转换?
最佳答案
我最终使用字符串表示 ViewModel 中的邮政编码,并在映射到我的域实体时进行了转换。
关于c# - 具有从字符串到类型的隐式转换的模型绑定(bind)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3707305/