使用[FromUri]属性 - 绑定嵌套数组复杂对象 [英] Using [FromUri] attribute - bind complex object with nested array

查看:679
本文介绍了使用[FromUri]属性 - 绑定嵌套数组复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送一个复杂的对象与嵌套数组的中的URI 以在GET请求一个MVC操作方法。

I want to send a complex object with a nested array in the uri to an MVC action method in a GET request.

考虑以下code:

 public ActionResult AutoCompleteHandler([FromUri]PartsQuery partsQuery){ ... }

 public class PartsQuery
 {
     public Part[] Parts {get; set; }
     public string LastKey { get; set; }
     public string Term { get; set; }
 }

 $.ajax({ 
    url: "Controller/AutoCompleteHandler", 
    data: $.param({                                        
                      Parts: [{ hasLabel: "label", hasType: "type", hasIndex : 1 }],
                      LastKey : "Last Key",
                      Term : "Term"                             
                   }),
    dataType: "json", 
    success: function(jsonData) { ... }
 });

这工作得很好,并结合正确使用默认的模型绑定的在MVC网页API

This works just fine and binds correctly using the default model binder in MVC Web Api.

不过,这个切换到纯MVC没有的WebAPI,默认模式粘结剂分解和嵌套数组中不能绑定的对象的属性:

However, switch this to plain MVC not WebApi and the default model binder breaks down and cannot bind the properties on objects in the nested array:

观察名单

partsQuery      != null          //Good
--LastKey       == "Last Key"    //Good
--Term          == "Term"        //Good
--Parts[]       != null          //Good
----hasLabel    == null          //Failed to bind
----hasType     == null          //Failed to bind
----hasIndex    == 0             //Failed to bind

我想知道为什么这打破了纯MVC以及如何使 FromUriAttribute 绑定该对象正确纯MVC

I would like to know why this breaks down in plain MVC and how to make FromUriAttribute bind this object correctly in plain MVC

推荐答案

在这里的核心问题是,MVC和的WebAPI使用不同型号的粘合剂。即使是基本接口是不同的。

Core issue here is that MVC and WebApi use different model binders. Even base interfaces are different.

Mvc - System.Web.Mvc.IModelBinder
Web API - System.Web.Http.ModelBinding.IModelBinder

当您发送您的$就呼叫数据,您发送以下查询字符串参数:

When you send data with your $.ajax call, you are sending following query string parameters:

Parts[0][hasLabel]:label
Parts[0][hasType]:type
Parts[0][hasIndex]:1
LastKey:Last Key
Term:Term

虽然,这将与MVC默认模型联编绑定正确的格式有参数名称不同的命名约定:

While, proper format that would bind with MVC default model binder has different naming convention for parameter names:

Parts[0].hasLabel:label
Parts[0].hasType:type
Parts[0].hasIndex:1
LastKey:Last Key
Term:Term

所以,这个方法调用将工作:

So, this method call would work:

$.ajax({ 
            url: "Controller/AutoCompleteHandler?Parts[0].hasLabel=label&Parts[0].hasType=type&Parts[0].hasIndex=1&LastKey=Last+Key&Term=Term",
            dataType: "json", 
            success: function(jsonData) { ... }
        });

您需要构建您的查询字符串尊重MVC模式的粘结剂命名约定。

You need to construct your query string respecting MVC model binder naming conventions.

此外 [FromUri] 在你的榜样action属性完全被忽略,因为它不知道MVC DefaultModelBinder。

Additionally [FromUri] attribute in your example action is completely ignored, since it's not known to MVC DefaultModelBinder.

这篇关于使用[FromUri]属性 - 绑定嵌套数组复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆