Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Dictionary<string, object> to MVC Controller

I am attempting to pass a javascript object ( key value pairs ) to an MVC Controller action using AJAX.

The controller action has a Dictionary parameter that receives the object.

[HttpPost]
public ActionResult SearchProject(IDictionary<string, object> filter ...

When the object in question is empty ( meaning its value in javascript is {} ), I see the following in my debugger.

enter image description here

Why are the controller and action names automatically added to the Dictionary parameter ?

Using fiddler I am able to see what is being passed to my controller and I do not see these 2 values being passed ..

If the javascript object is not empty, then everything works fine

I am stumped..

like image 691
G-Man Avatar asked Jan 26 '14 17:01

G-Man


People also ask

How to post a dict object to a MVC controller?

dict = new Object (); dict ['12'] = 5; dict ['13'] = 6; dict ['1000'] = 21; dict ['9'] = 13; dict ['13'] = 48; $.post ('/client.mvc/mypostaction/', { myDictionary: dict }); You can then post the dict object to your controller using a Dictionary<int, int> as property type.

How to pass data from tempdata dictionary to the sender action method?

In this technique, you store the data to be passed in the TempData dictionary in the sender action method. The receiving action method reads the data from the TempData dictionary. You might be aware that the TempData dictionary can hold data until it is read, and this can carry data across multiple requests. First we will create Customer Class.

How do I post data to a controller in MVC?

Posting Data to an MVC Controller With jQuery Posting data to an MVC controller with jQuery The easiest way to post data from a view to a controller action is by adding a form and submit button and letting most of the hard stuff happen automatically.

How to pass strongly typed data from controller to view using viewbag?

To pass the strongly typed data from Controller to View using ViewBag, we have to make a model class then populate its properties with some data and then pass that data to ViewBag with the help of a property. And then in the View, we can access the data of model class by using ViewBag with the pre-defined property.


1 Answers

It appends two values because by default MVC registers such ValueProviderFactory:

public sealed class RouteDataValueProviderFactory : ValueProviderFactory

That returns implementation of IValueProvider - RouteDataValueProvider:

public sealed class RouteDataValueProvider : DictionaryValueProvider<object>
{
    // RouteData should use the invariant culture since it's part of the URL, and the URL should be
    // interpreted in a uniform fashion regardless of the origin of a particular request.
    public RouteDataValueProvider(ControllerContext controllerContext)
        : base(controllerContext.RouteData.Values, CultureInfo.InvariantCulture)
    {
    }
}

Basically it just binds Dictionary to route values for current route.

For example if you add such data to routes in RouteConfig:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}",
    defaults: new { controller = "Home", action = "Index", SomeSpecificRouteData = 42 }
);

Then your Dictionary will have 3 values - controller, action and SomeSPecificRouteData.

Another sample is that you can define such action:

public ActionResult Index(string action, string controller, int SomeSpecificRouteData)

and RouteDataValueProvider will pass data from your route as parameters to these method. In that way MVC binds parameters from routes to actual parameters for an actions.

If you want to remove such behavior you just need to iterate over ValueProviderFactories.Factories and remove RouteDataValueProviderFactory from it. But then your routes can have issues with parameters binding.

like image 157
Sergey Litvinov Avatar answered Oct 26 '22 14:10

Sergey Litvinov