代码之家  ›  专栏  ›  技术社区  ›  Paul Suart Wes

如何从自定义模型绑定器中删除魔法字符串?

  •  5
  • Paul Suart Wes  · 技术社区  · 15 年前

    我现在已经编写了两个自定义模型绑定器,并意识到我已经落入了依赖魔法字符串的陷阱,例如:

        if (bindingContext.ValueProvider.ContainsPrefix("PaymentKey"))
        {
            paymentKey = bindingContext.ValueProvider.GetValue("PaymentKey").AttemptedValue;
        }
    

    谢谢。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Clicktricity    15 年前

    你要找的是 bindingContext.ModelName

     if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
        {
            paymentKey = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
        }