简体   繁体   English

单击“添加新记录”时,不触发Kendo Grid Popup

[英]Kendo Grid Popup not firing when Add New Record is clicked

I am using a Kendo Grid with popup editor to show rows from a database. 我正在使用带有弹出式编辑器的Kendo网格来显示数据库中的行。 The rows are from a "FemaleAdvice" table. 这些行来自“ FemaleAdvice”表。 Each FemaleAdvice object is going to belong to a FemaleCategory in the sense that FemaleCategory is a lookup table, and when the user selects a FemaleCategory, a list of FemaleAdvice object Titles and Descriptions will be shown. 从FemaleCategory是一个查找表的意义上讲,每个FemaleAdvice对象都将属于一个FemaleCategory,并且当用户选择FemaleCategory时,将显示FemaleAdvice对象的标题和描述列表。

In letting the user edit which FemaleCategory a FemaleAdvice object belongs to, it would make sense to display the Title of the category instead of the database Key. 在让用户编辑FemaleAdvice对象属于哪个FemaleCategory时,应该显示类别的标题而不是数据库Key。 I have taken a couple steps to do this, but right now I am getting a "Uncaught: ReferenceError: Category is not defined" error in the Console when I click on "Add new record". 我已经采取了一些步骤来执行此操作,但是当我单击“添加新记录”时,控制台中出现“未捕获:ReferenceError:类别未定义”错误。 I feel like this is something easy, but I am kind of new to MVC/Kendo, so my ignorance is showing. 我觉得这很容易,但是我对MVC / Kendo还是陌生的,所以我的无知正在显现。 My code is below: 我的代码如下:

    //FemaleAdvice Model
    [Key]
    [ScaffoldColumn(false)]
    public Int64 FemaleAdviceKey { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }

    [Required]
    [UIHint("FemaleCategoryKey")]
    [DisplayName("Female Category")]
    public Int64 FemaleCategoryKey { get; set; }
    public virtual FemaleCategory Category { get; set; }


    [UIHint("Body")]
    public string Body { get; set; }

    [ScaffoldColumn(false)]
    public DateTime LastUpdated { get; set; }


    //*************FemaleAdvice View**************
    @(Html.Kendo().Grid<com.RomanceCoachOnTheGo.MVC.Models.FemaleAdvice>()
    .Name("FemaleAdvice")
    .ToolBar(toolbar =>
    {
       toolbar.Create();
     })
    .DataSource(dataSource => dataSource
            .Ajax()
            .Model(item => { item.Id(m => m.FemaleAdviceKey); })

            .Create(c => c.Action("CreateFemaleAdvice", "Administrator"))
            .Read(r => r.Action("ReadFemaleAdvice", "Administrator"))
            .Update(u => u.Action("UpdateFemaleAdvice", "Administrator"))
            .Destroy(d => d.Action("DeleteFemaleAdvice", "Administrator"))
        )
    .Columns(col =>
            {
                col.Bound(c => c.FemaleCategoryKey).ClientTemplate("#=Category.Title#");
                col.Bound(c => c.Title);
                col.Bound(c => c.Description);
                col.Bound(c => c.Body);
                col.Command(command => { command.Edit(); command.Destroy(); });
            })
    .Editable(editing => editing.Mode(GridEditMode.PopUp))
    .Sortable()
    .Pageable()

    .Filterable()
     )



     //***********Relevant Controller Action***************
     public ActionResult ReadFemaleAdvice([DataSourceRequest] DataSourceRequest request)
    {
        List<FemaleAdvice> advice = _db.FemaleAdvice.Include("Category").AllActive().ToList();

        return Json(advice.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }


    public ActionResult CreateFemaleAdvice([DataSourceRequest] DataSourceRequest request, FemaleAdvice advice)
    {
        if (ModelState.IsValid)
        {
            advice.IsActive = true;
            _db.FemaleAdvice.Add(advice);
            _db.SaveChanges();
        }

        return Json(new[] { advice }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }

The problem is occurring because when you add a record your Category is null. 发生此问题的原因是,当您添加记录时,您的Category为空。 So Kendo doesn't know how to render the template you gave it here: 因此,剑道不知道如何渲染您在此处提供的模板:

col.Bound(c => c.FemaleCategoryKey).ClientTemplate("#=Category.Title#");

Unfortunately the Kendo template syntax doesn't do a good job of handling nulls, so you have to check for yourself. 不幸的是,Kendo模板语法不能很好地处理空值,因此您必须自己检查一下。 Change the template to something like this: 将模板更改为如下所示:

#= Category != null ? Category.Title : '' #

This will print out Category.Title if it is set, otherwise it will be an empty string until it is set. 这将打印出Category.Title如果已设置),否则它将是一个空字符串,直到它被设置为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM