5

I've written a couple of custom model binders now, and have realised that I've fallen into the trap of relying on magic strings, e.g.:

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

I'd like to be able to use an expression to strongly-type the prefix names, but can't figure out how, and would be grateful for some assistance.

Thanks.

Paul Suart
  • 6,505
  • 7
  • 44
  • 65
  • You could create a static class to hold those string values as properties and reference the properties instead. For example: bindingContext.ValueProvider.ContainsPrefix(SomeClass.PaymentKey) – Hector Correa Oct 05 '10 at 19:07

1 Answers1

1

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

 if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
    {
        paymentKey = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
    }
Clicktricity
  • 4,171
  • 1
  • 23
  • 15
  • Thanks for the answer, but no, this isn't what I'm looking for. Maybe my question could have been clearer, but I was looking for a way to avoid using magic strings for each property, not the model as a whole. Thanks anyway. – Paul Suart Oct 05 '10 at 10:45