开发者

How to rebind Json object with Telerik MVC grid

开发者 https://www.devze.com 2023-02-26 04:41 出处:网络
Im having problem with rebinding grid with Json object…. Im trying to create custom delete button…

Im having problem with rebinding grid with Json object…. Im trying to create custom delete button… So far I have Jquery function: Gets an ID of selected column (username) and call controller action “UserDetails”

Delete button:

$("#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!");
    }
});

This is action controller 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));
        }
    }

So far everything is working OK. But can I bind my grid with these Json data and how??? This is my Json result that I want to bind with grid...

And here is my grid:

@(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

精彩评论

暂无评论...
验证码 换一张
取 消