c# - 具有从字符串到类型的隐式转换的模型绑定(bind)属性

标签 c# asp.net-mvc modelbinders

我有以下类(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/

相关文章:

c# - 我如何知道执行更新的 SQL Server 存储过程是否有效?

c# - 如何构建一个 "defaulting map"数据结构

c# - Azure 队列客户端依赖项注入(inject)

c# - 将 View 模型列表传递给 mvc Controller 操作

asp.net-mvc - 当参数为模型时,ASP.NET MVC 发布文件模型绑定(bind)

c# - 如何引用实现某个接口(interface)的类?

c# - 获得对 "fall through"面板的点击

javascript - 如何在通过 Ajax.ActionLink 调用的动态加载的 ASP.NET MVC 部分 View 上初始化 jsTree

c# - 从 View 中检索数据,我应该使用模型联编程序吗?

asp.net-mvc - ASP.NET MVC 框架中的 MVVM 和 ModelBinders