0

I have a list with Strings of Display names of Model:

public class TVSystemViewData : BaseViewData
    {
        [Display(Name = "AccountId", Description = "")]
        public String AccountId { get; set; }

        [Display(Name = "AllocatedManagedMemory", Description = "")]
        public String AllocatedManagedMemory { get; set; }

        [Display(Name = "AllocatedPhysicalMemory", Description = "")]
        public String AllocatedPhysicalMemory { get; set; }

        [Display(Name = "AudioMute", Description = "")]
        public String AudioMute { get; set; }
}

I need to set the properties with a foreach loop to add the values to my Model:

This is how get values from POST the application

var model.AccountId = shell.getParameter("AccountId")
var model.AllocatedManagedMemory = shell.getParameter("AllocatedManagedMemory");

The shell.GetParameter get the value from a POST.

this is how i want: I have a a Method to get all Display attr

public List<String> GetTVSystemProperties()
{
    return typeof(TVSystemViewData)
        .GetProperties()
        .SelectMany(x => x.GetCustomAttributes(typeof(DisplayAttribute), true) //select many because can have multiple attributes
            .Select(e => ((DisplayAttribute)e))) //change type from generic attribute to DisplayAttribute
        .Where(x => x != null).Select(x => x.Name) //select not null and take only name
        .ToList();
}

My collection is a list of Strings ex: collection {"AccountId","AllocatedManagedMemory"...}

My model is TVSystemViewData

foreach (item in collection)
{
    if(item == modelProperty name){
         // i don know how
         model.property = shell.getParameter(item)
    }
}

[UPDATED] I am using this:

        foreach (var property in UtilsHandler.getConfigAsList("sysDataSource"))
        {
            //Set Values to Model
            try
            {
                model.GetType().GetProperty(property).SetValue(model, shell.getParameter(property), null);
            }
            catch (Exception)
            {
Have issue with data types 
            }
        }

I have issues with data types. But i use one foreach loop. Still looking for a best method

1
  • I am not sure if I understand the question. Are you writing a custom model binder? Commented Apr 20, 2018 at 15:58

1 Answer 1

0

you need to make the class inherit from IEnumerator, or add a GetEnumerator method yourself.

var model = new TVSystemViewData();

foreach(var item in model)
{
  item.AccountId = shell.getParameter("AccountId");
  //item.AllocatedManagedMemory ...
}

Please refer to this post for more information : How to make the class as an IEnumerable in C#?

Check this article out: https://support.microsoft.com/en-us/help/322022/how-to-make-a-visual-c-class-usable-in-a-foreach-statement

//EDIT. Forgot this part :

    List<TVSystemViewData> model;

    [Display(Name = "AccountId", Description = "")]
    public String AccountId { get; set; }

    [Display(Name = "AllocatedManagedMemory", Description = "")]
    public String AllocatedManagedMemory { get; set; }

    [Display(Name = "AllocatedPhysicalMemory", Description = "")]
    public String AllocatedPhysicalMemory { get; set; }

    [Display(Name = "AudioMute", Description = "")]
    public String AudioMute { get; set; }

    public IEnumerator<TVSystemViewData> GetEnumerator()
    {
        foreach (var item in model)
        {
            yield return item;
        }
    }

EDIT According to your update question: I don't know if this is the way to go but it should work.

var model = new TVSystemViewData();
        PropertyInfo[] properties = typeof(TVSystemViewData).GetProperties();
        List<string> items = new List<string> { "AccountId", "AllocatedManagedMemory" }; //your collection of strings


        foreach (var item in items)
        {
            foreach (var property in properties)
            {
                if (item == property.Name)
                {
                    property.SetValue(model, shell.getParameter(item));
                }
            }
        }
Sign up to request clarification or add additional context in comments.

5 Comments

I Update the question with more detail. In your Answer i have to do always model.AccountId = some; model.AudioMute=some; I have 68 properties to model
I have updated my answer, hope it helps. At least maybe point you in the right direction, also a similar topic, check it out if this doesn't help : stackoverflow.com/questions/8151888/…
I update with the code i use. remove one of your foreach.
You should add the error that you are getitng. I tried your updated thing and works fine. Does shell.gerParameter(item) return you a string? I suppose, but does the "UtilsHandler.getConfigAsList("sysDataSource")" give you the correct data type string? Why do you iterate that instead of your list of strings you mentioned?
the shell return always a string. you can surround with try catch. Or you can make a switch with datatypes and do a tryparse... this is just a ideia

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.