相关疑难解决方法(0)

使用[FromUri]属性 - 使用嵌套数组绑定复杂对象

我想在uri中将带有嵌套数组的复杂对象发送到GET请求中的MVC操作方法.

请考虑以下代码:

 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) { ... }
 });
Run Code Online (Sandbox Code Playgroud)

这很好用,并使用MVC Web Api中的默认模型绑定器正确绑定.

但是,将其切换为普通MVC而不是WebApi,并且默认模型绑定器会崩溃,并且无法绑定嵌套数组中对象的属性:

观察名单

partsQuery      != null          //Good
--LastKey       == "Last …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc model-binding

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

model-binding ×1