init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
112
KretaWeb/ModelBinder/DataSourceRequestModelBinder.cs
Normal file
112
KretaWeb/ModelBinder/DataSourceRequestModelBinder.cs
Normal file
|
@ -0,0 +1,112 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Web;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.ModelBinding;
|
||||
using System.Web.Script.Serialization;
|
||||
using Kendo.Mvc;
|
||||
using Kendo.Mvc.Infrastructure;
|
||||
using Kendo.Mvc.UI;
|
||||
using Kreta.Web.Helpers.Grid;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Kreta.Web.ModelBinder
|
||||
{
|
||||
public class DataSourceRequestModelBinder : IModelBinder
|
||||
{
|
||||
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
|
||||
{
|
||||
//sort = ID - asc & page = 1 & pageSize = 20 & group = &filter =
|
||||
if (actionContext.Request.Method != HttpMethod.Post)
|
||||
{
|
||||
var request = new KretaGridDataSourceRequest();
|
||||
|
||||
if (TryGetValue(bindingContext, GridUrlParameters.Sort, out string sort))
|
||||
{
|
||||
request.Sorts = GridDescriptorSerializer.Deserialize<SortDescriptor>(sort);
|
||||
}
|
||||
|
||||
if (TryGetValue(bindingContext, GridUrlParameters.Page, out int currentPage))
|
||||
{
|
||||
request.Page = currentPage;
|
||||
}
|
||||
|
||||
if (TryGetValue(bindingContext, GridUrlParameters.PageSize, out int pageSize))
|
||||
{
|
||||
request.PageSize = pageSize;
|
||||
}
|
||||
|
||||
if (TryGetValue(bindingContext, GridUrlParameters.Filter, out string filter))
|
||||
{
|
||||
request.Filters = FilterDescriptorFactory.Create(filter);
|
||||
}
|
||||
|
||||
if (TryGetValue(bindingContext, GridUrlParameters.Group, out string group))
|
||||
{
|
||||
request.Groups = GridDescriptorSerializer.Deserialize<GroupDescriptor>(group);
|
||||
}
|
||||
|
||||
if (TryGetValue(bindingContext, GridUrlParameters.Aggregates, out string aggregates))
|
||||
{
|
||||
request.Aggregates = GridDescriptorSerializer.Deserialize<AggregateDescriptor>(aggregates);
|
||||
}
|
||||
|
||||
bindingContext.Model = request;
|
||||
}
|
||||
else
|
||||
{
|
||||
var jsonString = ExtractRequestJson(actionContext);
|
||||
JObject json = (JObject)JsonConvert.DeserializeObject(jsonString);
|
||||
var d = new Dictionary<string, string>(json.ToObject<IDictionary<string, string>>(), StringComparer.CurrentCultureIgnoreCase);
|
||||
var request = new KretaGridDataSourceRequest();
|
||||
|
||||
request.Sorts = d.ContainsKey("sort")
|
||||
? GridDescriptorSerializer.Deserialize<SortDescriptor>(d["sort"])
|
||||
: null;
|
||||
request.Page = d.ContainsKey("page") ? int.Parse(d["page"]) : 1;
|
||||
request.PageSize = d.ContainsKey("pageSize") ? int.Parse(d["pageSize"]) : 0;
|
||||
request.Groups = d.ContainsKey("group")
|
||||
? GridDescriptorSerializer.Deserialize<GroupDescriptor>(d["group"])
|
||||
: null;
|
||||
request.Aggregates = d.ContainsKey("aggregates")
|
||||
? GridDescriptorSerializer.Deserialize<AggregateDescriptor>(d["aggregates"])
|
||||
: null;
|
||||
request.Filters = d.ContainsKey("filter") ? FilterDescriptorFactory.Create(d["filter"]) : null;
|
||||
request.data = d.ContainsKey("data") ? d["data"] : null;
|
||||
bindingContext.Model = request;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryGetValue<T>(ModelBindingContext bindingContext, string key, out T result)
|
||||
{
|
||||
var value = bindingContext.ValueProvider.GetValue(key);
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
result = default(T);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
result = (T)value.ConvertTo(typeof(T));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string ExtractRequestJson(HttpActionContext actionContext)
|
||||
{
|
||||
var content = actionContext.Request.Content;
|
||||
var query = content.ReadAsStringAsync().Result;
|
||||
var dict = HttpUtility.ParseQueryString(query);
|
||||
var json = new JavaScriptSerializer().Serialize(
|
||||
dict.AllKeys.ToDictionary(k => k, k => dict[k]));
|
||||
return json;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue