Checking to see if ViewBag has a property or not, to conditionally inject JavaScript

后端 未结 5 1101
轮回少年
轮回少年 2020-12-09 02:04

Consider this simple controller:

Porduct product = new Product(){
  // Creating a product object;
};
try
{
   productManager.SaveProduct(product);
   return          


        
5条回答
  •  春和景丽
    2020-12-09 02:14

    Your code doesnt work because ViewBag is a dynamic object not a 'real' type.

    the following code should work:

    public static bool Has (this object obj, string propertyName) 
    {
        var dynamic = obj as DynamicObject;
        if(dynamic == null) return false;
        return dynamic.GetDynamicMemberNames().Contains(propertyName);
    }
    

提交回复
热议问题