如何使用Telerik MVC网格重新绑定Json对象

时间:2011-04-16 00:57:15

标签: json jquery asp.net-ajax telerik-grid telerik-mvc

m having problem with rebinding grid with Json object…. I尝试创建自定义删除按钮... 到目前为止,我有Jquery函数:获取所选列的ID(用户名)和调用控制器操作“UserDetails”

删除按钮:

$("#DeleteUser").click(function () {
    if (id != "") {
        var answer = confirm("Delete user " + id)
        if (answer) {
            $.ajax({
                type: "POST",
                url: "/Admin/UserDetails",
                data: "deleteName=" + id,
                success: function (data) {

                }
            });
        }
    } else {
        $("#erorMessage").html("First you must select user you whant to delete!");
    }
});

这是动作控制器UserDetails(string startsWith,string deleteName)

[GridAction]
    public ActionResult UserDetails(string startsWith, string deleteName)
    {   // Custom search...
        if (!string.IsNullOrEmpty(startsWith))
        {
            return GetSearchUserResult(startsWith);
        }
        if (!string.IsNullOrEmpty(deleteName))
        {
            TblUserDetails user = db.TblUserDetails.Single(a => a.TblUser.userName == deleteName);
            try
            {
                TblUser userToDelete = db.TblUser.Single(a => a.userId == user.TblUser.userId);
                db.DeleteObject(user);
                db.DeleteObject(userToDelete);

                db.SaveChanges();
                Membership.DeleteUser(deleteName);

                List<UserDto> retModelData = new List<UserDto>();
                //GetAllUsers() returns a List<UserDto> of users.
                retModelData = GetAllUsers();

                var model = new GridModel
                {
                    Data = retModelData,
                    Total = GetAllUsers().Count()
                };
                return View(model);
            }
            catch
            {
                return View(new GridModel());
            }
        }

        else
        {
            var user = GetAllUsers();
            return View(new GridModel(user));
        }
    }

到目前为止一切正常。但我可以用这些Json数据绑定我的网格以及如何??? 我希望与网格绑定的This is my Json result ...

这是我的网格:

@(Html.Telerik().Grid<App.Web.Models.UserDto>()
.Name("Grid")
.DataKeys(key =>
{
    key.Add(a => a.Id);
})
.Columns(column =>
{
    column.Bound(a => a.Username).Filterable(false);
    column.Bound(a => a.FirstName).Filterable(false);
    column.Bound(a => a.LastName).Filterable(false);
    column.Bound(a => a.Email).Filterable(false);
})
.DetailView(detailView => detailView.ClientTemplate(
                "<table id='DetailTable'><tbody><tr class='UserRow'><td class='Tbllable'><b>First name</b></td><td><#= FirstName #></td>"
                        + "<td></td><td></td>"
                        + "</tr><tr><td class='Tbllable'><b>Last name</b></td>"
                        + "<td><#= LastName #></td>"
                        + "<td id='Roles'></td><td id='Operations'></td>"
                        + "</tr><tr><td class='Tbllable'><b>Username</b></td><td><#= Username #></td></tr><tr><td class='Tbllable'><b>Address</b></td>"
                        + "<td><#= Address #></td></tr><tr><td class='Tbllable'><b>Email</b></td><td><#= Email #></td></tr><tr><td class='Tbllable'><b>Birth date</b></td>"
                        + "<td></td></tr><tr><td class='Tbllable'><b>Registration date</b></td><td></td></tr><tr><td class='Tbllable'><b>Phone number</b></td>"
                        + "<td><#= PhoneNumberHome #></td></tr><tr><td class='Tbllable'><b>Mobile number</b></td><td><#= PhoneNumberMobile #></td></tr></tbody></table>"
                ))
//.EnableCustomBinding(true)
.DataBinding(bind => bind.Ajax().Select("UserDetails", "Admin", new { startsWith = ViewBag.startsWith }))
.Pageable(paging =>
        paging.PageSize(12)
              .Style(GridPagerStyles.NextPreviousAndInput)
              .Position(GridPagerPosition.Bottom)
    )
.ClientEvents(e => e
    .OnRowDataBound("Expand")
    .OnRowSelect("select")
    .OnLoad("replaceConfirmation")
)
    .RowAction(row =>
    {
        if (row.Index == 0)
        {
            row.DetailRow.Expanded = true;
        }
    })
.Editable(editing => editing.Mode(GridEditMode.PopUp))
.Selectable()
.Sortable()

0 个答案:

没有答案