简体   繁体   English

将Dictionary <string,object>传递给MVC Controller

[英]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. 我试图使用AJAX将javascript对象(键值对)传递给MVC Controller操作。

The controller action has a Dictionary parameter that receives the object. 控制器操作具有接收对象的Dictionary参数。

[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. 当有问题的对象为 (意味着它在javascript中的值为{})时,我在调试器中看到以下内容。

在此输入图像描述

Why are the controller and action names automatically added to the Dictionary parameter ? 为什么控制器和操作名称会自动添加到Dictionary参数中?

Using fiddler I am able to see what is being passed to my controller and I do not see these 2 values being passed .. 使用fiddler我能够看到传递给我的控制器的内容,我没有看到传递这两个值。

If the javascript object is not empty, then everything works fine 如果javascript对象不为空,那么一切正常

I am stumped.. 我很难过..

It appends two values because by default MVC registers such ValueProviderFactory: 它附加两个值,因为默认情况下MVC注册了ValueProviderFactory:

public sealed class RouteDataValueProviderFactory : ValueProviderFactory

That returns implementation of IValueProvider - RouteDataValueProvider : 返回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. 基本上它只是将Dictionary绑定到当前路由的路由值。

For example if you add such data to routes in RouteConfig : 例如,如果您将此类数据添加到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 . 那么你的Dictionary将有3个值 - controlleractionSomeSPecificRouteData

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. RouteDataValueProvider将路由中的数据作为参数传递给这些方法。 In that way MVC binds parameters from routes to actual parameters for an actions. 以这种方式,MVC将路径中的参数绑定到动作的实际参数。

If you want to remove such behavior you just need to iterate over ValueProviderFactories.Factories and remove RouteDataValueProviderFactory from it. 如果要删除此类行为,只需迭代ValueProviderFactories.Factories并从中删除RouteDataValueProviderFactory But then your routes can have issues with parameters binding. 但是,您的路线可能会出现参数绑定问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM