如何在 Ajax 绑定上传递复杂类型数据

发布于 2024-12-04 20:59:50 字数 1528 浏览 0 评论 0原文

Telerik ASP.NET MVC Q2 2011

我有一个 Ajax 绑定的网格:

@(Html.Telerik().Grid<IUser>()
                .Name("UsersGrid")
                .DataBinding(binding => binding.Ajax().Select("UsersGridSelect", "User", new { area = "Admin" }))
                .ClientEvents(events => events.OnDataBinding("onGridRefresh"))
     ...
     ...

这是我的 onGridRefresh 处理程序:

function onGridRefresh(e)
{
    var modelData =
    {
        Username:       $('#SearchModel_Username').val(),
        FirstName:      $('#SearchModel_FirstName').val(),
        LastName:       $('#SearchModel_LastName').val(),   
        IsLocked:       $('#SearchModel_IsLocked').is(':checked'),
        ClientId:       $('#SearchModel_ClientId').val(),
        UserCode:       $('#SearchModel_UserCode').val(),
        ExpireDateFrom: $('#SearchModel_ExpireDateFrom').val(),
        ExpireDateTo:   $('#SearchModel_ExpireDateTo').val()
    };

    e.data = { searchModel: modelData };
}

这是我的控制器:

[GridAction]
public ActionResult UsersGridSelect(UserSearchModel searchModel)
{
    IList<IUser> userList = searchModel == null ? new List<IUser>() : _repository.GetUsers(searchModel);

    return View(new GridModel(userList));
}

我知道这个设置不起作用......但至少它演示了我所追求的。 当我将我的值作为单独的参数传递时,它可以工作:

public ActionResult UsersGridSelect(string Username, string Firstname, string LastName,....)

但我更愿意将它作为对象传递。有什么办法可以做到这一点吗?

Telerik ASP.NET MVC Q2 2011

I have a grid which is Ajax bound:

@(Html.Telerik().Grid<IUser>()
                .Name("UsersGrid")
                .DataBinding(binding => binding.Ajax().Select("UsersGridSelect", "User", new { area = "Admin" }))
                .ClientEvents(events => events.OnDataBinding("onGridRefresh"))
     ...
     ...

This is my onGridRefresh handler:

function onGridRefresh(e)
{
    var modelData =
    {
        Username:       $('#SearchModel_Username').val(),
        FirstName:      $('#SearchModel_FirstName').val(),
        LastName:       $('#SearchModel_LastName').val(),   
        IsLocked:       $('#SearchModel_IsLocked').is(':checked'),
        ClientId:       $('#SearchModel_ClientId').val(),
        UserCode:       $('#SearchModel_UserCode').val(),
        ExpireDateFrom: $('#SearchModel_ExpireDateFrom').val(),
        ExpireDateTo:   $('#SearchModel_ExpireDateTo').val()
    };

    e.data = { searchModel: modelData };
}

And this is my controller:

[GridAction]
public ActionResult UsersGridSelect(UserSearchModel searchModel)
{
    IList<IUser> userList = searchModel == null ? new List<IUser>() : _repository.GetUsers(searchModel);

    return View(new GridModel(userList));
}

This setup does not work, I know... But at least it demonstrates what I am after.
When I pass in my values as seperate parameter it works:

public ActionResult UsersGridSelect(string Username, string Firstname, string LastName,....)

But I would prefere to pass it in as an object. Is there any way to accomplish that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

焚却相思 2024-12-11 20:59:50

如果你使用
e.data = modelData
它会正确绑定。

If you use
e.data = modelData
it will bind correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文