How to remove magic strings from user model bindings?

I wrote a couple of user bindings to the model now and realized that I was trapped by relying on magic strings, for example:

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

I would like to use the expression for strong input of prefix names, but I can not understand how I will be grateful for the help.

Thank.

+5
source share
1 answer

What you are looking for is this bindingContext.ModelName, so your code could become:

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

All Articles