简体   繁体   English

ASP.NET MVC5 json值绑定到脚手架

[英]Asp.net MVC5 json value binding to scafolding

I am using SyncFusion Asp.Net MVC grid, in this I am trying to filter at server side and the json sending to server is as below 我正在使用SyncFusion Asp.Net MVC网格,在此我尝试在服务器端进行过滤,并且发送到服务器的json如下所示

but in the ViewModel where object propertis are coming as null not binding 但是在ViewModel中,对象属性以null的形式出现而不是绑定

Json 杰森

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

I have created models as below and this is passing to controller but it is not binding. 我创建了如下模型,并将其传递给控制器​​,但未绑定。

 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; }
    }

Controller Method 控制器方式

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

   }

The JSON you are sending doesn't seem to match your view model at all: 您发送的JSON似乎根本与您的视图模型不匹配:

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

Consider writing a view model that will match this structure: 考虑编写一个符合以下结构的视图模型:

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; }
}

and now your controller action could take this view model as parameter: 现在您的控制器操作可以将此视图模型用作参数:

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

I am not familiar with SyncFusion Asp.Net MVC grid which you seem to be using, but you should make sure that the Content-Type: application/json request HTTP header is sent along with the AJAX request as well so that the ASP.NET MVC model binder knows the content type sent from the client. 我对您似乎正在使用的SyncFusion Asp.Net MVC grid不熟悉,但是您应该确保Content-Type: application/json请求HTTP标头也与AJAX请求一起发送,以便ASP.NET MVC模型绑定程序知道从客户端发送的内容类型。 Use the developer toolbar of your webbrowser or a tool such as Fiddler to inspect the request sent from the client and ensure that this header is present. 使用Web浏览器的开发人员工具栏或Fiddler之类的工具来检查从客户端发送的请求,并确保存在此标头。 Otherwise, the view model won't be bound. 否则,将不会绑定视图模型。

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

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