简体   繁体   中英

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

but in the ViewModel where object propertis are coming as null not binding

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:

{
    "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. 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. Otherwise, the view model won't be bound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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