带有多选列的Kendo mvc网格。行是空引用

时间:2018-02-23 09:23:35

标签: javascript asp.net-mvc entity-framework kendo-ui kendo-grid

我有两个关系“多对多”的模型,书籍,作者及其上下文库。所有源文件都包含正确。无法将选定的作者设置为单元格(因为行为空,因此无法设置模型)。有使用InCell编辑器。我会很高兴任何想法)

UPD:我添加新行时出现新错误

无法获取未定义或空引用的属性“map”

Index.cs.html:(更改函数中的错误,dataBound函数中的第二个错误)

 @{
    ViewBag.Title = "Home Page";
}
@(Html.Kendo().Grid<ManyToMany.Models.ViewModel>()
                .Name("grid")
                .Columns(columns =>
                {
                    columns.Bound(e => e.BookId).Visible(false);
                    columns.Bound(e => e.Pages);
                    columns.Bound(e => e.Genre);
                    columns.Bound(e => e.Publisher);
                    columns.Bound(e => e.Authors).ClientTemplate(

                         Html.Kendo().MultiSelect().Name("multi#=BookId#")
                            .DataTextField("AuthorName")
                            .DataValueField("AuthorId")
                            .BindTo((IEnumerable<ManyToMany.Models.Author>)ViewData["authors"]) /*TaskSecond.Models.ViewModel*/
                            .Events(e => e.Change("change"))
                            .ToClientTemplate().ToHtmlString()
                        );
                    columns.Command(command => command.Destroy()).Width(150);

                })
                .ToolBar(toolbar =>
                {
                    toolbar.Create();
                    toolbar.Save();
                })
                .Editable(editable => editable.Mode(GridEditMode.InCell))
                                                    .Pageable()
                                                    .Filterable()
                .Events(e => e.DataBound("dataBound"))
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .Batch(true)
                .Model(model =>
                {
                    model.Id(p => p.BookId);
                    model.Field(p => p.Pages).DefaultValue(0);
                    model.Field(p => p.Genre).DefaultValue(0);
                    model.Field(p => p.Publisher).DefaultValue("");
                    model.Field(p => p.Authors).DefaultValue(
                        ViewData["defaultAuthors"] as ManyToMany.Models.Author);
                })
                    .PageSize(5)
                     .Read(read => read.Action("Books_Read", "Grid"))
        .Create(create => create.Action("Books_Create", "Grid"))
         .Update(update => update.Action("Books_Update", "Grid"))
        .Destroy(destroy => destroy.Action("Books_Destroy", "Grid"))
                    .ServerOperation(false))
)

<script type="text/javascript">
    function dataBound(e) {
        var grid = this;
        grid.tbody.children().each(function () {
            var dataitem = grid.dataItem(this),
                row = $(this);
            eval(row.find("script").html());
            var multiSelect = $(this).find("select").data("kendoMultiSelect");
            //here
            multiSelect.value(dataitem.Authors.map(function (i) { return i.AuthorId }));
        });
    }

    function change(e) {
//error is here
        var row = this.element.closest("tr"),
            model = $("#grid").data("kendoGrid").dataItem(row);
        model.set("Authors", this.dataItems());
    }

</script>

ViewModel.cs:

 public class ViewModel
{
    public ViewModel()
    {
        Authors = new List<Author>();
    }
    public int BookId { get; set; }

    public string BookName { get; set; }

    public int Pages { get; set; }

    public Genre Genre { get; set; }

    public string Publisher { get; set; }
    [UIHint("AuthorsEditor")]
    public List<Author> Authors { get; set; }
}

1 个答案:

答案 0 :(得分:0)

尝试在change()javascript函数中使用 - this.select()而不是this.element.closest。您可能需要优化以下代码以满足您的要求,但它可以让您了解在调试时要更改的内容:

var row = this.select();
if (row.length > 0 )
{
    selectedRow = e.sender.select();
    var item = e.sender.dataItem(selectedRow);
}