简体   繁体   中英

Telerik kendo ui mvc dropdownlist in grid to when in edit mode, and show value when in display mode

So i have a Kendo UI grid and i have one column that is a int

basically have a int column and

1 means good, 2 means best, 3 means top,

Its kind of thing that a Enum would be the right thing to use but i cant (internal business rules) so i need to make a custom drop down that will replace the number

there are 2 ways to do it in mvc

one is to use a EditorTemplateName and the other one is to use a ForeignKey

the problem is that EditorTemplateName only works in edit mode and displays the number in display mode, and ForeignKey only works in display mode and displays the number in edit mode

ok after some hard work here is what i came up with

i used a ClientTemplate together with EditorTemplateName

columns.Bound(c => c.Condition)
.EditorTemplateName("_ConditionDropDown")
       .ClientTemplate("# if (Condition=== 1) {#" +
                    "good" +
                "#} else if (Condition=== 2) {#" +
                    "best" +
                "#} else if (Condition=== 3) {#" +
                    "top" +
                "#}#");

And It Works as i needed


i decided to add the template i used it might help someone

@{
    ViewBag.ConditionDropDownList = new List<SelectListItem>()
                {
                    new SelectListItem { Value="0", Text="- All Conditions -" },
                    new SelectListItem { Value="1", Text="good" },
                    new SelectListItem { Value="2", Text="best" },
                    new SelectListItem { Value="3", Text="top" }
                };
}

@(Html.Kendo().DropDownList()
              .Name("Condition")
           .DataTextField("Text")
           .DataValueField("Value")
               .BindTo((System.Collections.IEnumerable)ViewBag.ConditionDropDownList)
               .Value("0")
)

this way will work sometimes, if not the second answer will.

A dropdownlist should be used by default for the foreignkey column. If a dropdownlist is not used in your project then this indicates that the GridForeignKey editor template is missing from the EditorTemplates folder. If that is the case the you should copy the editor from the [Installation folder] > wrappers > aspnetmvc > EditorTemplates > razor folder to the EditorTemplates folder in your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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