get Get property from ViewModel

All we need is an easy explanation of the problem, so here it is.

I have one method for push class property into NameValuCollection

private NameValueCollection ObjectToCollection(object objects)
{

    NameValueCollection parameter = new NameValueCollection();


    Type type = objects.GetType();

    PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance |
                                                    BindingFlags.DeclaredOnly |
                                                    BindingFlags.Public);
    foreach (PropertyInfo property in properties)
    {
        if (property.GetValue(objects, null) == null)
        {
            parameter.Add(property.Name.ToString(), "");
        }
        else
        {
            if (property.GetValue(objects, null).ToString() != "removeProp")
            {
                parameter.Add(property.Name.ToString(), property.GetValue(objects, null).ToString());
            }
        }
    }

    return parameter;
    }

in my case when I pass My Model class to this method it’s for correctly,but when in my Model class I use another Model like this

public class Brand
{
    public MetaTags MetaTag { get; set; } // <---- Problem is here

    public string BrandName { get; set; }

}

public class MetaTags
{
    public string Title { get; set; }

    public string Description { get; set; }

    public string Language { get; set; }
}

it’s not add MetaTags Class Property to the collection and just Add MetaTag to the collection

I want this method return this OutPut

key:Title       Value:value
key:Description Value:value
key:Language    Value:value
key:BrandName   Value:value

but this method return this

key:MetaTag     Value:
key:BrandName   Value:value

how i can do it ?
very thank for your help

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Before adding a empty string, check whether the current property is MetaTags or not. If so, use this function recursively.

private NameValueCollection ObjectToCollection(object objects)
{

NameValueCollection parameter = new NameValueCollection();


Type type = objects.GetType();

PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance |
                                                BindingFlags.DeclaredOnly |
                                                BindingFlags.Public);
foreach (PropertyInfo property in properties)
{
    if (property.PropertyType == typeof(MetaTags))
    {
            parameter.Add(property.Name.ToString(),ObjectToCollection(property.GetValue(objects, null)))
    }
    else{
    if (property.GetValue(objects, null) == null)
    {
        parameter.Add(property.Name.ToString(), "");
    }
    else
    {
        if (property.GetValue(objects, null).ToString() != "removeProp")
        {
            parameter.Add(property.Name.ToString(), property.GetValue(objects, null).ToString());
        }
    }
    }
}

return parameter;
}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply