1

I am getting Values from a form in a Dictionary. Key equals field on the page and value is what the user has supplied .Is there a better way to pass the values to an object as below :I am new to this so will appreciate your help . Thanks for your help
I get the values using the following call :

public async void PostFormData()

Now I am trying to pass the values from the dictionary to an Object as below . The method below is very elementary and I am looking to make this more dynamic .

public static void ConverttoObject()
                {
                    Dictionary<string, string> test = new Dictionary<string, string>();

                    test.Add("Name", "Daniel");
                    test.Add("LastName", "Wong");
                    test.Add("Zip", "60004");
                    test.Add("City", "New York");


                    var abc = new FormInfo();

                    foreach (var key in test.Keys)
                    {
                        foreach (var val in test.Values)
                        {
                            if (key.Equals("Name"))
                                abc.Name = val;
                            else if (key.Equals("LastName"))
                                abc.City = val;
                            else if (key.Equals("Zip"))
                                abc.Zip = val;
                            else if (key.Equals("City"))
                                abc.City = val;

                        }
                    }
                }
            }

            public class FormInfo
            {
                public string Name { get; set; }
                public string LastName { get; set; }
                public string Zip { get; set; }
                public string City { get; set; }

            }
user1110790
  • 787
  • 2
  • 8
  • 27
  • What do you mean _getting form values from javascript in Dictionary_? What is you client side code. How are you posting the form values. What is the signature of your POST method? –  Sep 28 '15 at 23:48
  • foreach (var key in provider.FormData.AllKeys) { foreach (var val in provider.FormData.GetValues(key)) { Trace.WriteLine(string.Format("{0}: {1}", key, val)); } } – user1110790 Sep 28 '15 at 23:50
  • 3
    If your POST method is `public ActionResult Edit(Form model)` then the model is correctly bound for you. What is it that you trying to do? –  Sep 28 '15 at 23:51
  • I am trying to POST from to server Side .I am using public async void PostFormData() – user1110790 Sep 28 '15 at 23:54
  • Your not making any sense. There is no such thing as a Dictionary in javascript. And what is your javascript? Edit you question to explain what it is you trying to do. If your submitting a form based on typeof `Form`, and you method has a parameter for `Form`, then its all bound by the framework. –  Sep 28 '15 at 23:58
  • Sorry about that , I have edited my question and added more details . I understand the confusion . Thanks – user1110790 Sep 29 '15 at 00:09
  • I think you're still missing something. Just make your action method `public ActionResult Edit(FormInfo model)` and MVC will handle it all for you automatically - just make sure the form field names (or Json properties) match the property names on the object and you're done. – Basic Sep 29 '15 at 00:10
  • 1
    I think you need to go to the MVC site and work through some tutorials to understand the basics of MVC –  Sep 29 '15 at 00:11
  • Have a read through [this article](http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx), it explains model binding and shows a number of approaches that vary from very manual (still simpler than what you're attempting) all the way through to near-automatic – Basic Sep 29 '15 at 00:17

2 Answers2

2

You could make your own extension method to convert a dictionary to an object like this one:

public static class IDictionaryExtensions
{
    public static T ToObject<T>(this IDictionary<string, object> source)
        where T : class, new()
    {
        T someObject = new T();
        Type someObjectType = someObject.GetType();

        foreach (KeyValuePair<string, object> item in source)
        {
            someObjectType.GetProperty(item.Key).SetValue(someObject, item.Value, null);
        }

        return someObject;
    }
}

Solution approach here.

Community
  • 1
  • 1
0

Couldn't you do:

abc.Name = test["Name"];
abc.LastName = test["LastName"];
abc.Zip = test["Zip"];
abc.City = test["City"];
Buzinas
  • 11,597
  • 2
  • 36
  • 58