如何在kendo网格中使用枚举

时间:2018-02-28 07:14:46

标签: javascript c# entity-framework kendo-grid kendo-multiselect

抱歉我的英文。 我有一个枚举,我的网格只显示数值。我怎样才能使用枚举的文本变体?这是一个非常普遍的问题,但我仍然没有为我找到合适的解决方案。 我很乐意为这个问题提供任何帮助)

Index.cshtml

@{
    ViewBag.Title = "Book List";
}
<script type="text/kendo" id="authorsTemplate">
    <ul>
        # for(var i = 0; i < Authors.length; i++){ #
        <li>#: Authors[i].AuthorName #</li>
        # } #
    </ul>
</script>

<div id="grid"></div>
<script>
    function authorsEditor(container, options) {
        $('<input name="Authors">').appendTo(container)
            .kendoMultiSelect({
                dataValueField: "AuthorId",
                dataTextField: "AuthorName",
                dataSource: @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["authors"]))
            });
    }

    $("#grid").kendoGrid({
        pageable: true,
        toolbar: ["create"],
        dataSource: {
            pageSize: 3,
            transport: {
                read: {
                    url: "@Url.Action("Read", "Grid")",
                    type: "POST"
                },
                update: {
                    url: "@Url.Action("Update", "Grid")",
                    contentType: "application/json",
                    type: "POST"
                },
                create: {
                    url: "@Url.Action("Create", "Grid")",
                    contentType: "application/json",//hz nado li
                    type: "POST"
                },
                 destroy: {
                    url: "@Url.Action("Destroy", "Grid")",
                    contentType: "application/json",//hz nado li
                    type: "POST"
                },
                parameterMap: function(options) {
                    return kendo.stringify(options);
                }
            },
            schema: {
                model: {
                    id: "BookId",
                    fields: {
                        AuthorName: { type: "string" }
                    }
                }
            }
        },
        editable: "inline",
        columns: [
            "BookName",
            "Pages",
        //    { editor: GenreEditor, field: "Genre" },IEnumerable<Number2.Models.AuthorViewModel>
            "Genre",
            "Publisher",
            {
                editor: authorsEditor, field: "Authors", template: $("#authorsTemplate").html()
               
            },
            { command: ["edit", "destroy"] }
        ]
    });
</script>

ViewModel和Enum (典型的viewmodel和enum)

namespace Number2.Models
{
    public class BookViewModel
    {
        public BookViewModel()
        {
            Authors = new List<AuthorViewModel>();
        }
        [ScaffoldColumn(false)]
        public int BookId { get; set; }

        [Display(Name = "Book Name")]
        [MaxLength(100, ErrorMessage = "Book Name must be 100 characters or less"), MinLength(5)]
        public string BookName { get; set; }
        public int Pages { get; set; }

        public string Publisher { get; set; }

        [Range(0, maximum: 10)]
        public Genre Genre { get; set; }

        [UIHint("AuthorsEditor")]
        public virtual List<AuthorViewModel> Authors { get; set; }
        public void CopyToBook(Book book)
        {
            book.BookId = BookId;
            book.BookName = BookName;
            book.Pages = Pages;
            book.Genre = Genre;
            book.Publisher = Publisher;
        }
    }
    public enum Genre
    {
        [Description("Comedy")]
        Comedy,
        [Description("Drama")]
        Drama,
        [Description("Horror")]
        Horror,
        [Description("Realism")]
        Realism,
        [Description("Romance")]
        Romance,
        [Description("Satire")]
        Satire,
        [Description("Tragedy")]
        Tragedy,
        [Description("Tragicomedy")]
        Tragicomedy,
        [Description("Fantasy")]
        Fantasy,
        [Description("Mythology")]
        Mythology,
        [Description("Adventure")]
        Adventure,
    }
}

问题仍然相关...... 我认为我的枚举应该使用特殊的viewmodel,但我不知道如何正确地做到这一点。

0 个答案:

没有答案