Pau*_*art 5 asp.net-mvc model-binding magic-string
我现在写了几个自定义模型绑定器,并意识到我已陷入依赖魔术字符串的陷阱,例如:
if (bindingContext.ValueProvider.ContainsPrefix("PaymentKey"))
{
paymentKey = bindingContext.ValueProvider.GetValue("PaymentKey").AttemptedValue;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够使用表达式强类型化前缀名称,但无法弄清楚如何,并将感谢一些帮助.
谢谢.
您正在寻找的是bindingContext.ModelName这样您的代码可以变成:
if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
{
paymentKey = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
363 次 |
| 最近记录: |