Rip*_*ppo 5 asp.net-mvc kendo-ui
我正在使用远程加载数据的Telerk Kendo UI网格.将QueryString传递到我的操作方法如下: -
take=10&skip=0&page=1&pageSize=10&sort[0][field]=value&sort[0][dir]=asc
Run Code Online (Sandbox Code Playgroud)
我试图找出如何将sort参数绑定到我的方法?是否可以或者我需要QueryString手动枚举集合还是创建自定义绑定器?
到目前为止,我试过这个: -
public JsonResult GetAllContent(int page, int take, int pageSize, string[] sort)
public JsonResult GetAllContent(int page, int take, int pageSize, string sort)
Run Code Online (Sandbox Code Playgroud)
但排序始终为空.有谁知道我怎么能做到这一点?
我可以回退到Request.QueryString使用,但这有点像kludge.
var field = Request.QueryString["sort[0][field]"];
var dir = Request.QueryString["sort[0][dir]"];
Run Code Online (Sandbox Code Playgroud)
您可以使用一组字典:
public ActionResult Index(
int page, int take, int pageSize, IDictionary<string, string>[] sort
)
{
sort[0]["field"] will equal "value"
sort[0]["dir"] will equal "asc"
...
}
Run Code Online (Sandbox Code Playgroud)
然后定义自定义模型绑定器:
public class SortViewModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelName = bindingContext.ModelName;
var keys = controllerContext
.HttpContext
.Request
.Params
.Keys
.OfType<string>()
.Where(key => key.StartsWith(modelName));
var result = new Dictionary<string, string>();
foreach (var key in keys)
{
var val = bindingContext.ValueProvider.GetValue(key);
result[key.Replace(modelName, "").Replace("[", "").Replace("]", "")] = val.AttemptedValue;
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
将在Global.asax中注册:
ModelBinders.Binders.Add(typeof(IDictionary<string, string>), new SortViewModelBinder());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2859 次 |
| 最近记录: |