我现在已经编写了几个自定义模型绑定(bind)器,并且意识到我已经陷入了依赖魔术字符串的陷阱,例如:
if (bindingContext.ValueProvider.ContainsPrefix("PaymentKey"))
{
paymentKey = bindingContext.ValueProvider.GetValue("PaymentKey").AttemptedValue;
}
我希望能够使用表达式来强输入前缀名称,但无法弄清楚如何,并且会感谢您的帮助。
谢谢。
最佳答案
你要找的是bindingContext.ModelName
所以你的代码可以变成:
if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
{
paymentKey = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
}
关于asp.net-mvc - 如何从自定义模型绑定(bind)器中删除魔术字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3854161/