2010-10-04 20 views
5

我已經寫了幾個自定義模型粘合劑,現在,已經意識到我已經陷入了依靠神奇的字符串,如陷阱:如何從定製模型活頁夾中移除魔術字符串?

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

我希望能夠使用表達式強類型的前綴名稱,但無法弄清楚如何,並會感謝一些幫助。

謝謝。

+0

您可以創建一個靜態類來保存這些字符串值作爲屬性和引用屬性,而不是。例如:bindingContext.ValueProvider.ContainsPrefix(SomeClass.PaymentKey) – 2010-10-05 19:07:23

回答

1

你所尋找的是bindingContext.ModelName因此您的代碼將變成:

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

感謝您的答案,但不,這不是我正在尋找的。也許我的問題可能會更清楚,但我正在尋找一種方法來避免爲每個屬性使用魔術字符串,而不是整個模型。不管怎麼說,還是要謝謝你。 – 2010-10-05 10:45:34