【问题标题】:Asp.net MVC5 json value binding to scafoldingAsp.net MVC 5 json 值绑定到脚手架
【发布时间】:2014-07-06 17:39:58
【问题描述】:

我正在使用 SyncFusion Asp.Net MVC 网格,在此我试图在服务器端进行过滤,发送到服务器的 json 如下

但在 ViewModel 中,对象属性作为 null 不绑定

Json

{"select":["Area","Id"],"where":[{"isComplex":false,"field":"Area","operator":"startswith","value":"test","ignoreCase":true}],"sorted":[{"name":"Area","direction":"ascending"}]}

我已经创建了如下模型,它正在传递给控制器​​,但它没有绑定。

 public class UserViewModel
    {
        public int skip { get; set; }
        public int take { get; set; }
        public Sort sorted { get; set; }
        public string[] group { get; set; }
        //Grid Action Params;
        public string action { get; set; }
        public string key { get; set; }
        public string keyColumn { get; set; }
        public string[] select { get; set; }
        public Search search { get; set; }
        public Where where { get; set; }
        public ApplicationUser value { get; set; }
    }

    public class Where
    {
        public bool isComplex { get; set; }
        public string field { get; set; }
        public string @operator { get; set; }
        public string @value { get; set; }
        public bool ignoreCase { get; set; }

    }
    public class Sort
    {
        public string name { get; set; }
        public string direction { get; set; }
     //   "sorted":[{"name":"Area","direction":"ascending"}],"group":["Area"]
    }

    public class Search
    {
        public string[] fields { get; set; }

        public string @operator { get; set; }

        public string key { get; set; }

        public bool ignoreCase { get; set; }
    }

控制器方法

 public async Task<ActionResult> DataSource(UserViewModel editParams)
   {

   }

【问题讨论】:

    标签: c# jquery asp.net asp.net-mvc json


    【解决方案1】:

    您发送的 JSON 似乎与您的视图模型完全不匹配:

    {
        "select": [
            "Area",
            "Id"
        ],
        "where": [
            {
                "isComplex": false,
                "field": "Area",
                "operator": "startswith",
                "value": "test",
                "ignoreCase": true
            }
        ],
        "sorted": [
            {
                "name": "Area",
                "direction": "ascending"
            }
        ]
    }
    

    考虑编写一个与此结构匹配的视图模型:

    public class UserViewModel
    {
        public string[] Select { get; set; }
        public Where[] where { get; set; }
        public Sorted[] sorted { get; set; }
    }
    
    public class Where
    {
        public bool IsComplex { get; set; }
        public string Field { get; set; }
        public string Operator { get; set; }
        public string Value { get; set; }
        public bool IgnoreCase { get; set; }
    }
    
    public class Sorted
    {
        public string Name { get; set; }
        public string Direction { get; set; }
    }
    

    现在您的控制器操作可以将此视图模型作为参数:

    public async Task<ActionResult> DataSource(UserViewModel editParams)
    {
        ...
    }
    

    我不熟悉您似乎正在使用的 SyncFusion Asp.Net MVC grid,但您应该确保 Content-Type: application/json 请求 HTTP 标头也与 AJAX 请求一起发送,以便 ASP.NET MVC 模型绑定器知道从客户端发送的内容类型。使用 Web 浏览器的开发人员工具栏或 Fiddler 等工具检查从客户端发送的请求并确保存在此标头。否则,视图模型将不会被绑定。

    【讨论】:

      猜你喜欢
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多