【问题标题】:ASP.net and CustomModelBinder and a prefixASP.net 和 CustomModelBinder 和一个前缀
【发布时间】:2009-06-30 18:58:48
【问题描述】:

使用默认的 ModelBinder 及其属性 Bind,我们可以像这样设置前缀:

public ActionResult Save([Bind(Prefix="test")] Person p)) {

}

我有一个 CustomModelBinderAttribute,它返回一个定制的 ModelBinder:

public ActionResult Save([PersonBinderAttribute(Prefix="test2")] Person p)) {

}

如何从我的定制模型绑定器中访问 Prefix 的值?

【问题讨论】:

    标签: asp.net-mvc model


    【解决方案1】:

    我不相信你可以。我会将前缀声明为控制器顶部的常量,然后使用它。

    private const string c_prefix = "test2";
    public ActionResult Save([PersonBinderAttribute(Prefix=c_prefix)] Person p)) {
        var prefix = c_prefix;
    }
    

    据我了解,您在 Attribute 中声明的任何内容都只能由 Attribute 使用。

    【讨论】:

    • 我可以访问控制器中的值!它在我被难住的模型绑定器中。然而,它很容易实现——只需向 PersonBinderAttribute 添加一个前缀属性,然后在创建模型绑定器时将值传递给它——但是我希望框架本机支持客户绑定器的前缀注释。它似乎没有。
    • 我知道你来自哪里。实际上我昨天用 ActionFilterAttribute 做了类似的事情,但没有与你的问题建立联系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2014-05-27
    • 2015-03-15
    相关资源
    最近更新 更多