How do I pass a Dictionary as a parameter to an ActionResult method from jQuery/Ajax?

后端 未结 6 1537
无人共我
无人共我 2020-11-30 05:27

I\'m using jQuery to make an Ajax call using an Http Post in ASP.NET MVC. I would like to be able to pass a Dictionary of values.

The closest thing I could think of

6条回答
  •  感动是毒
    2020-11-30 05:50

    This is what I tried. Saves a lot of work. Javascript:

      var dict = {};       
            dict["id"] = "200";
            dict["FirstName"] = "Chris";
            dict["DynamicItem1"] = "Some Value";
            dict["DynamicItem2"] = "Some Other Value";
    
            var theObject = {};
            theObject.dict = dict;
            $.post(URL, theObject, function (data, textStatus, XMLHttpRequest) {
                console.log("success");
            }, "json");
    

    Action Method:

    public ActionResult MethodName(DictionaryModel obj)
        {
           //Action method logic
        }
    
    public class DictionaryModel
    {
        public Dictionary dict { get; set; }
    
    }
    

提交回复
热议问题