简体   繁体   English

Asp.Net Core中的Kendo Grid中的DropDownList

[英]DropDownList in Kendo Grid in Asp.Net Core

I need to implement simple dropdown list to choose predefined cultures. 我需要实现简单的下拉列表来选择预定义的文化。

My grid: 我的网格:

@(Html.Kendo().Grid<NewLibrary.ViewModels.BookViewModel>()
    .Name("booksGrid")
    .Columns(column =>
    {
        column.Bound(m => m.Name);
        column.Bound(m => m.Culture).EditorTemplateName("CultureSelectorTemplate");
    })
    .ToolBar(toolBar =>
    {
        toolBar.Create();
        toolBar.Save();
    })
    .Sortable()
    .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(10)
    )
    .HtmlAttributes(new { style = "border-style: double; border-width: 5px" })
    .Editable(e => e.Mode(GridEditMode.InCell))
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .ServerOperation(false)
        .Model(m =>
        {
            m.Id(f => f.BookId);
            m.Field(f => f.Name);
            m.Field(f => f.Culture);
        })
        .Create(create => create.Action("CreateBooks", "Books"))
        .Read(read => read.Action("ReadBooks", "Books"))
        .Update(update => update.Action("UpdateBooks", "Books"))
        .Destroy(destroy => destroy.Action("DeleteBooks", "Books"))
    )
)

My editor template in /Shared/EditorTemplates: 我在/ Shared / EditorTemplates中的编辑器模板:

@(Html.Kendo().DropDownList()
    .Name("Culture")
    .DataTextField("Text")
    .DataValueField("Value")
    .BindTo(new List<SelectListItem>()
    {
        new SelectListItem()
        {
            Text = "English",
            Value = "en"
        },
        new SelectListItem()
        {
            Text = "Spanish",
            Value = "es"
        },
        new SelectListItem()
        {
            Text = "French",
            Value = "fr"
        }
    })
)

My viewmodel: 我的viewmodel:

public class BookViewModel
{
    public string BookId { get; set; }

    public string Name { get; set; }

    public string Culture { get; set; }
}

The problem is, that I can't bind values from dropdown list to model, when I choose them from list, and then going to edit another book, the value from list disappear. 问题是,我无法将值从下拉列表绑定到模型,当我从列表中选择它们,然后再编辑另一本书时,列表中的值将消失。

What's the problem with this implementation, how can I do it correct, while googling dozens of same answers, gave me nothing at all. 这个实现的问题是什么,我怎么能纠正它,而谷歌搜索几十个相同的答案,什么也没给我任何东西。

So, what is the right way to implement DropDownList in Kendo Grid via Asp.Net Core? 那么,通过Asp.Net Core在Kendo Grid中实现DropDownList的正确方法是什么?

Ok, how it must be. 好的,它一定是怎么回事。

My view: 我的观点:

@(Html.Kendo().Grid<BookViewModel>()
        .Name("booksGrid")
        .Columns(column =>
        {
            column.Bound(m => m.Name);
            column.Bound(m => m.Culture).ClientTemplate("#=Culture.NativeName#").EditorTemplateName("CultureSelectorTemplate");
        })
        .ToolBar(toolBar =>
        {
            toolBar.Create();
            toolBar.Save();
        })
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(10)
        )
        .HtmlAttributes(new { style = "border-style: double; border-width: 5px" })
        .Editable(e => e.Mode(GridEditMode.InCell))
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .ServerOperation(false)
            .Model(m =>
            {
                m.Id(f => f.BookId);
                m.Field(f => f.Name);
                m.Field(f => f.Culture).DefaultValue(ViewData["defaultCulture"] as CultureViewModel);//new SelectListItem() { Text = "English", Value = "en" });
            })
            .Create(create => create.Action("CreateBooks", "Books"))
            .Read(read => read.Action("ReadBooks", "Books"))
            .Update(update => update.Action("UpdateBooks", "Books"))
            .Destroy(destroy => destroy.Action("DeleteBooks", "Books"))
        )
        .Events(e => e.DataBound("onGridDataBound"))
    )

My viewmodels: 我的观点模型:

 public class BookViewModel
    {
        public string BookId { get; set; }

        public string Name { get; set; }

        public CultureViewModel Culture { get; set; }
    }

 public class CultureViewModel
    {
        public string NativeName { get; set; }

        public string TwoLetterCode { get; set; }
    }

My editor template: 我的编辑器模板:

@model CultureViewModel
@(Html.Kendo().DropDownListFor(m => m)
    .DataTextField("NativeName")
    .DataValueField("TwoLetterCode")
    .BindTo(new List<CultureViewModel>()
    {
        new CultureViewModel()
        {
            NativeName = "English",
            TwoLetterCode = "en"
        },
        new CultureViewModel()
        {
            NativeName = "Spanish",
            TwoLetterCode = "es"
        },
        new CultureViewModel()
        {
            NativeName = "French",
            TwoLetterCode = "fr"
        }
    })
)

At last, you must populate you default value in ViewData in Index method, or, in grid's DefaultValue directly. 最后,您必须在Index方法中填充ViewData中的默认值,或者直接在网格的DefaultValue填充默认值。

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

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