[英]ASP.NET MVC model binding - nested properties
I have a model: 我有一个模型:
public class DataModel
{
public GridSortOptions SortOptions { get; set; }
public string Term { get; set; }
public int? Page { get; set; }
...
}
public class GridSortOptions
{
public string Column { get; set; }
public SortDirection Direction { get; set; }
}
And a http request: ?Column=LastName&Direction=Descending
和一个http请求: ?Column=LastName&Direction=Descending
That request will not work, i need to update it as follow: ?SortOptions.Column=LastName&SortOptions.Direction=Descending
该请求将不起作用,我需要按以下方式更新它: ?SortOptions.Column=LastName&SortOptions.Direction=Descending
Is it possible to teach ASP.NET to understand the original request without changing the model? 可以教ASP.NET理解原始请求而无需更改模型吗?
PS. PS。 I know that I could create a custom binder but I feel that there is a much simple way exist... 我知道我可以创建一个自定义活页夹,但是我觉得这里存在一种非常简单的方法...
Thanks in advance! 提前致谢!
I have mine structured slightly differently, as I came across the same problem (in MVC1 and still have it now - mvc4) . 我遇到的相同问题(在MVC1中,现在仍然是-mvc4)使我的结构略有不同。 I have often found that having nested models tend to cause headaches and unnecessary complexity, so I try to flatten my view models as much as possible, just to simplify things, so don't have to write custom model binders, saves a lot of time and code. 我经常发现使用嵌套模型会引起麻烦和不必要的复杂性,因此我尝试尽可能地扁平化我的视图模型,只是为了简化操作,所以不必编写自定义模型绑定程序,从而节省了大量时间和代码。
My action typically looks method looks like this 我的动作通常看起来像这样
//...snip
public ActionResult List(int? page, GridSortOptions sortOptions, string keyword) {
var model = new UserGridViewModel();
IQueryable<User> users = new UserRepository(Session).List();
if (sortOptions.Column != null) {
users = users.OrderBy(sortOptions.Column, sortOptions.Direction);
}
if (keyword != null) {
users = users.Where(x => x.Name.Contains(keyword))
}
model.SortOptions = sortOptions;
//using MvcContrib.Pagination.PaginationHelper here
model.Results = users.AsPagination(page ?? 1, 20);
return View(model);
}
//.....
My view logic is simply: 我的视图逻辑很简单:
@using MvcContrib.UI.Grid
@using MvcContrib.UI.Pager
@model UsersGridViewModel
@Html.Grid(Model.Results).Columns(cols => {
cols.For(col => col.Id).Sortable(true);
cols.For(col => col.Name).Sortable(true);
//...etc
}).Sort(Model.SortOptions)
My grid view models are normally like this: 我的网格视图模型通常是这样的:
public class UserGridViewModel
{
public IPagination<User> Results { get; set; }
public GridSortOptions SortOptions { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.