具有从字符串到类型的隐式转换的模型绑定属性
我有以下类:
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
对象之外,在发送到操作方法时,地址中的所有内容都会正确绑定。这似乎是因为它存储为 PostCode
而不是字符串。
我怎样才能强迫模型绑定者尊重这个演员?
I have the following class:
public class PostCode {
public string Name { get; set; }
public static implicit operator PostCode(string postCode)
{
return new PostCode {Name = postCode};
}
}
It forms part of an Address
class which is the model for a helper template (EditorTemplates>Address.ascx).
This helper template is rendered using <%= Html.EditorFor(model => model.Address)%>
where Address
is the property on another object.
Everything in the address is correctly bound when posting to the action method apart from the PostCode
object. It seems likely that this is due to the fact that it is stored as a PostCode
instead of a string.
How can I force the model binder to honour this cast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终使用一个字符串来表示 ViewModel 中的邮政编码,并在映射到我的域实体中进行了转换。
I ended up using a string to represent the postcode in the ViewModel and did the conversion in the mapping to my domain entity.
您有邮政编码的编辑器模板吗?
如果没有创建它。
Did you have EditorTemplate for PostCode?
If not create it.