【问题标题】:kendo ui grid - newbie can't get basic sample to work剑道 ui 网格 - 新手无法让基本样本工作
【发布时间】:2013-07-16 18:02:46
【问题描述】:

我熟悉 Telerik 产品,但对 Kendo UI MVC 包装器不熟悉。几个月前我最初下载了它们,但从未真正尝试过它们,很遗憾我的试用支持已过期。

我已经在 VS.NET 中启动了一个全新的 Kendo UI MVC 应用程序。

我创建了自己的控制器和视图。

我复制了批处理编辑示例 (http://demos.kendoui.com/web/grid/editing.html),并为示例使用的数据换出了一个静态列表。

网格正确显示我的数据。

但是,单击保存时,更新不会向服务器 ({}) 发送任何内容。单击保存时,删除也不会向服务器 ({}) 发送任何内容。为每个新项目创建发送记录,但 Name 属性设置为 null。

有什么想法吗?

我的代码如下。

谢谢, 凯文

* 我的模特 *

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KendoUIMvcApplication1.Models
{
    public class PersonalInterestModel
    {
        public int ID;
        public string Name;
    }
}

* 我的控制器 *

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using KendoUIMvcApplication1.Models;


namespace KendoUIMvcApplication1.Controllers
{
    public class ManagerController : Controller
    {
        //
        // GET: /Manager/
        private static List<PersonalInterestModel> items = new List<PersonalInterestModel>();

        static ManagerController()
        {
            items.Add(new PersonalInterestModel() { ID = 1, Name = "Finance" });
            items.Add(new PersonalInterestModel() { ID = 2, Name = "Construction" });
            items.Add(new PersonalInterestModel() { ID = 3, Name = "Technology" });
            items.Add(new PersonalInterestModel() { ID = 4, Name = "Entertainment" });
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Editing_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(items.ToDataSourceResult(request));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Editing_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<PersonalInterestModel> products)
        {
            var results = new List<PersonalInterestModel>();

            if (products != null && ModelState.IsValid)
            {
                foreach (var product in products)
                {
                    product.ID = items.Max(i => i.ID) + 1;
                    items.Add(product);
                    results.Add(product);
                }
            }

            return Json(results.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Editing_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<PersonalInterestModel> products)
        {
            if (products != null && ModelState.IsValid)
            {
                foreach (var product in products)
                {
                    var target = items.Find(p => p.ID == product.ID);
                    if (target != null)
                    {
                        target.Name = product.Name;
                    }
                }
            }

            return Json(ModelState.ToDataSourceResult());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Editing_Destroy([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<PersonalInterestModel> products)
        {
            if (products.Any())
            {
                foreach (var product in products)
                {
                    items.Remove(items.Find(p => p.ID == product.ID));
                }
            }

            return Json(ModelState.ToDataSourceResult());
        }

    }
}

* 我的观点 *

@{
    ViewBag.Title = "Personal Interests";
}

<h2>Index</h2>

@(Html.Kendo().Grid<KendoUIMvcApplication1.Models.PersonalInterestModel>()    
    .Name("Grid")    
    .Columns(columns => {        
        columns.Bound(p => p.ID).Width(50);
        columns.Bound(p => p.Name).Width(140);
        columns.Command(command => command.Destroy()).Width(110);
    })
    .ToolBar(toolbar => {
        toolbar.Create();
        toolbar.Save();        
    })
    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .Pageable()
    .Sortable()
    .Scrollable()
    .DataSource(dataSource => dataSource        
        .Ajax()         
        .Batch(true)
        .ServerOperation(false)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(p => p.ID))
        .Create("Editing_Create", "Manager")
        .Read("Editing_Read", "Manager")
        .Update("Editing_Update", "Manager")
        .Destroy("Editing_Destroy", "Manager")
    )
)
<script type="text/javascript">
    function error_handler(e) {    
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function() {
                        message += this + "\n";
                    });
                }
            });        
            alert(message);
        }
    }
</script>

【问题讨论】:

    标签: kendo-ui


    【解决方案1】:

    我只是在我的项目中尝试你的代码,它工作得很好,只需这样做,

    型号

     public class PersonalInterestModel
        {
            public int ID {get; set;}
            public string Name { get; set; }
        }
    

    【讨论】:

    • 试过了,它似乎修复了创建(名称值不再作为null传递,但它无助于更新和删除。更新和删除仍然将空的JSON对象发送到服务器。
    • 所以当我最初尝试时,它似乎无法完全正常工作。但是今天早上我一定很累,没有改变任何东西,它工作得很好。谢谢!该死的那些setter/getter!!
    • 我很高兴它对你有用。请掩饰为答案,以便其他人遇到同样的问题。
    • @Jaimin 一个问题我想问你他在上面的问题产品中提到...在创建方法中我只是复制并粘贴代码并且它对我不起作用它说产品不在当前上下文中...(如果您有空闲时间)请澄清一下..
    • @pratapk:对不起,我只是不明白你的问题。产品不在当前上下文中。你正在使用哪个产品。请在评论中显示一些代码,以便我理解你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多