Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through model properties and get value in code file

In my application I have created a method that sorts my data and creates a list that I pass to my data layer. I have overloaded it to accept params object[] and a model. I am writing my overloaded method that accepts a model but I am having problems looping through it.

This is my controller method

        [HttpPost]
        public ActionResult CreateUser(vw_UserManager_Model model)
        {
            // Return Model to view with error message when not valid.
            if (!ModelState.IsValid == true)
            {
                return View(model);
            }
            else
            {
                List<string> myParams = DataCleaner.OrganizeParams(model);
            }

This is my method for organizing the data before passing it to Data layer

public static List<string> OrganizeParams(vw_UserManager_Model model)
        {
            List<string> myParams = new List<string>();

            var modelProperties = model.GetType().GetProperties();

            foreach (var property in model.GetType().GetProperties())
            {
                switch (property.PropertyType.Name)
                {
                    case "String":
                        myParams.Add("System.String" + ":" + property.GetValue(property.PropertyType.Name, null));
                        break;
                    case "Guid":
                        myParams.Add("System.Guid" + ":" + property.GetValue(property.PropertyType.Name, null));
                        break;
                    case "Int32":
                        myParams.Add("System.Int32" + ":" + property.GetValue(property.PropertyType.Name, null));
                        break;
                    case "Boolean":
                        myParams.Add("System.Boolean" + ":" + property.GetValue(property.PropertyType.Name, null));
                        break;
                }
            }
            return myParams;
        }

What I do in my Switch/Case logic doesn't actually work because I viewed my object in a breakpoint and couldn't see what I need to write in code. I know I can use IEnumerable as well but I am not quite sure how I want to do it.

Any suggestions?

Summary

How to loop through model in code file in MVC3?

like image 422
nick gowdy Avatar asked Sep 07 '25 09:09

nick gowdy


1 Answers

This worked for me, looks like you were pretty close though :)

public static List<string> OrganizeParams(vw_UserManager_Model model)
{
    List<string> myParams = new List<string>();

    foreach (var property in model.GetType().GetProperties())
    {
        switch (property.PropertyType.GenericTypeArguments.FirstOrDefault().Name.ToString())
        {
            case "String":
                myParams.Add("System.String" + ":" + property.GetValue(model, null));
                break;
            case "Guid":
                myParams.Add("System.Guid" + ":" + property.GetValue(model, null));
                break;
            case "Int32":
                myParams.Add("System.Int32" + ":" + property.GetValue(model, null));
                break;
            case "Boolean":
                myParams.Add("System.Boolean" + ":" + property.GetValue(model, null));
                break;
        }
    }
    return myParams;
}
like image 103
Dannyh Avatar answered Sep 10 '25 09:09

Dannyh