How to check if ViewBag has a certain dynamically created property or not?-VBForums
Results 1 to 3 of 3

Thread: How to check if ViewBag has a certain dynamically created property or not?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    36

    Question How to check if ViewBag has a certain dynamically created property or not?

    Folks,

    Suppsoe that a certain dynamic property of a ViewBag object is optional. How can I check whether it has been created or not?
    My first reaction is to check for null value.
    Code:
    @if (ViewBag.OptProp != null) {...}
    But then how to handle the case when the property has been actually created, but the value assigned to it is null ?

    Any suggestion, insight or reference is really appreciated!

    Cheers,
    - Nick

    p.s. I think, my question is similar to this one: How do I check if an object has a property in JavaScript?. Except mine is about .NET .
    Last edited by kender; Feb 3rd, 2015 at 04:07 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,086

    Re: How to check if ViewBag has a certain dynamically created property or not?

    How do you usually determine whether an object has a property in C#? Using Reflection. You'd start with ViewBag.GetType().GetProperty(...).

    That said, I'd suggest that you use two properties. The first would be the value for OptProp and the second would be whether or not OptProp has been set, if you really need to distinguish between not set and set to null. You can then do this:
    Code:
    @if (ViewBag.IsOptPropSet && ViewBag.OptProp != null)
    The controller action must then ALWAYS set IsOptPropSet to either true or false and, if true, it must then set OptProp.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: How to check if ViewBag has a certain dynamically created property or not?

    If you have multiple fields you can create a methode, that uses reflection, just like jmcilhinney said:

    Code:
    public static bool HasProperty (object obj, string propertyName) 
    {
    var result = false;
        var dynamic = obj as DynamicObject;
        if(dynamic == null)
        {
            result = false;
        }
        else
        {
    result = dynamic.GetDynamicMemberNames().Contains(propertyName);
    
        }
        return result;}
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.