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

时间:2014-06-27 21:44:08

标签: c# jquery asp.net-mvc kendo-ui kendo-grid

我使用带有弹出编辑器的Kendo Grid来显示数据库中的行。行来自“FemaleAdvice”表。在FemaleCategory是查找表的意义上,每个FemaleAdvice对象将属于一个FemaleCategory,当用户选择一个FemaleCategory时,将显示一个FemaleAdvice对象标题和描述的列表。

在让用户编辑一个FemaleAdvice对象所属的FemaleCategory时,显示类别的标题而不是数据库密钥是有意义的。我已经采取了几个步骤来做到这一点,但是当我点击“添加新记录”时,我在控制台中收到“未捕获:ReferenceError:类别未定义”错误。我觉得这很容易,但我对MVC / Kendo有点新意,所以我的无知正在显现。我的代码如下:

    //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);
    }

1 个答案:

答案 0 :(得分:0)

问题出现了,因为当您添加记录时,Category为空。所以剑道不知道如何渲染你在这里给出的模板:

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

不幸的是,Kendo模板语法在处理空值方面做得不好,所以你必须自己检查。将模板更改为以下内容:

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

如果已设置,将打印出Category.Title,否则在设置之前它将为空字符串。