如何解决在ASP.NET MVC剑道UI电网与CRUD操作问题 [英] How to resolve issues with CRUD Operations in an ASP.NET MVC Kendo UI Grid

查看:190
本文介绍了如何解决在ASP.NET MVC剑道UI电网与CRUD操作问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让剑道UI电网作为一个系统,我目前正在写一个用户管理工具。我数据绑定到网格,使用ASP.NET身份来获取用户信息,但我似乎无法获得更新或删除操作火的网格。

I've been trying to get the Kendo UI grid to act as a user management tool in a system I'm currently writing. I've bound data to the grid, using ASP.NET Identity to get the user information, but I can't seem to get the update or delete actions to fire on the grid.

我已经定格了,如下:

    @(Html.Kendo().Grid<MyProject.Models.UserInfo>()
    .Name("userGrid")
    .Columns(columns =>
    {
        columns.Bound(p => p.UserName);
        columns.Bound(p => p.FirstName);
        columns.Bound(p => p.LastName);
        columns.Bound(p => p.Region);
        columns.Bound(p => p.Roles);
        columns.Command(command => { command.Edit(); command.Destroy(); command.Custom("ViewDetails").Click("showDetails"); });
    })
    .Filterable()
    .Sortable()
    .Navigatable()
    .Resizable(r => r.Columns(true))
    .Editable(editable => { editable.Mode(GridEditMode.InLine); editable.DisplayDeleteConfirmation("Are you sure you want to delete this user?"); })
    .HtmlAttributes(new { style = "min-height:90px;max-height:450px;" })
    .DataSource(dataSource => dataSource
        .Ajax() 
        .Model(model =>
        {
            model.Id(p => p.UserId);
            model.Field(p => p.UserId).Editable(false);
            model.Field(p => p.FirstName).Editable(true);
            model.Field(p => p.LastName).Editable(true);
            model.Field(p => p.UserName).Editable(true);
            model.Field(p => p.Roles).Editable(false);
            model.Field(p => p.Region).Editable(false);
        }
        ).Read(read => read.Action("GetAllUsers", "Admin"))
        .Update(update => update.Action("UpdateUser", "Admin"))
        .Destroy(update => update.Action("DeleteUser", "Admin"))
    )
)

凡我模型定义为:

Where my model is defined as:

public class UserInfo
{
    public string UserId {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public string UserName {get;set;}
    public string Roles {get;set;}
    public string Region {get;set;}
}

和我AdminController包含下列方法:

And my AdminController contains the following methods:

        [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult GetAllUsers([DataSourceRequest]DataSourceRequest request)
    {
        using (var context = new ApplicationDbContext())
        {
            var users = context.Users.ToList();
            var moreUsers = users.Select(x => new UserInfo { UserName = x.UserName, UserId = x.Id, FirstName = x.FirstName, LastName = x.LastName, Region = x.RegionId.ToString(), Roles = string.Join(", ", x.Roles.Select(p => p.Role.Name).ToList()) }).ToList();
            return Json(moreUsers.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
    }

    [AcceptVerbs(HttpVerbs.Post)]        
    public ActionResult UpdateUser([DataSourceRequest] DataSourceRequest request, UserInfo user)
    {
        if (user != null && ModelState.IsValid)
        {
            //userService.Update(user);
        }

        return Json(new[] { user }.ToDataSourceResult(request, ModelState));
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult DeleteUser([DataSourceRequest] DataSourceRequest request, UserInfo user)
    {
        if (user != null && ModelState.IsValid)
        {
            //userService.Update(user);
        }

        return Json(new[] { user }.ToDataSourceResult(request, ModelState));
    }

目前,当我点击删除按钮,我收到404未找​​到错误并通过网格执行的操作的格式为一个GET:本地主机/ MyProject的/行政/ DeleteUser用户ID = X,名字= Y ......等等。我不能为我的生活工作为什么发生这种情况,因为我觉得我跟着的例如载于演示 pretty准确。

Currently, when I click the "Delete" button, I receive a 404 not found error and the action performed by the grid is a GET in the format : localhost/MyProject/Admin/DeleteUser?UserId=x,FirstName=y... etc. I can't for the life of me work out why this is happening as I think I've followed the example set out in the demos pretty accurately.

有没有人有任何帮助我吗?

Has anyone got any help for me?

推荐答案

该问题涉及到电网被执行GET,而不是POST的事实,但不幸被hutchonoid提供的解决方案没有奏效。这改变了所有的网格行动的职位,包括GET操作。然而,它也让我在寻找的解决方案的好地方。

The issue was related to the fact the grid was performing a GET and not a POST, but unfortunately the solution provided by hutchonoid didn't work. That changed all the grid actions to posts, including the GET action. However, it did get me looking in the right place for the solution.

这是给出的答复另一个问题实际上解决了问题。为了让我的工作网格,我明确定义网格行动应该是什么样的行动:

An answer given to another question actually resolved the issue. To get my grid working, I explicitly defined what action the grid actions should be:

.Read(read => read.Action("GetAllUsers", "Admin").Type(HttpVerbs.Get))
.Update(update => update.Action("UpdateUser", "Admin").Type(HttpVerbs.Post))
.Destroy(update => update.Action("DeleteUser", "Admin").Type(HttpVerbs.Post))

我还不能完全肯定,为什么Telerik的演示作品,未经定义的操作的类型,但我猜它是关系到脚本。也许jQuery的版本我使用?

I'm still not entirely sure why the Telerik demo works without defining the type of the action, but I'm guessing it is related to the scripts. Perhaps the version of jQuery I'm using?

这篇关于如何解决在ASP.NET MVC剑道UI电网与CRUD操作问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆