2013-05-20 30 views
0

在剃刀視圖中,如何檢查模型中是否存在屬性?MVC如何檢查model.property中的NULL

if (Model._myProp != null) <--- error .RuntimeBinder.RuntimeBinderException if Model does not contain _myProp 
    { 
     ...do something 
    } 
+0

可能的重複http://stackoverflow.com/questions/3468250/c-sharp-elegant-way-to-check-if-a-propertys-property-is-null –

+0

請停止提及「ASP。 NET MVC「簡稱爲」MVC「。一個是框架,而另一個是獨立於語言的設計模式。這就像打電話給IE - 「互聯網」 –

回答

1

你不想檢查,該模型的屬性之一具有要檢查它有一個屬性或不和ofcourse您解決方案將無法工作的方式,但@ Satpal的習慣類型的值無效也工作。

使用強類型視圖並將不同的屬性分隔到接口中,並在局部視圖中使用它們。