How to pass dictionary from JQuery Ajax to C# MVC Model

By 4th September 2019JQuery, MVC

We have a model class in mvc c#, that also contains a dictionary property, and we want to pass data in this dictionary from jquery ajax.

Here we go,
We have a Model Class

 public class ModelClass
        private Dictionary<string, string> _parameter= new Dictionary<string, string>();
        public Dictionary<string, string> Parameter{ get { return _parameter; } set { _parameter= value; } }

And Here is Ajax and jquery code to send the dictionary as parameter

var parameter = [];
            parameter.push({ "key": "FromDate", "value": $('#FromDate').val() });
            parameter.push({ "key": "ToDate", "value": $('#ToDate').val() });
            var jsonParameters = { "Parameter": parameter };

                type: "POST",
                url: '@Url.Action("Home","Index")',
                contentType: 'application/json',
                data: JSON.stringify({ ModelClass: jsonParameters }),
                success: function (data) {
                error: function () {

And Here is controller that will accept parameters in ModelClass

public class HomeController : Controller
        public ActionResult Index(ModelClass model) // Here You will get Dictionary as parameter
            return View();