简体   繁体   中英

Kendo UI add DropDownList into Grid (MVC)

I'm Binding to a Grid class (UserId,FirstName,LastName,Choice). Does anybody know how to put this code in a column (Choice) in a Kendo Grid in MVC 4:

@(Html.Kendo().DropDownList()
      .Name("Test")
      .DataTextField("Text")
      .DataValueField("Value")
      .Events(e => e.Change("change"))
      .BindTo(new List<SelectListItem>()
      {
          new SelectListItem()
          {
              Text = "Option1",
              Value = "1"
          },
          new SelectListItem()
          {
              Text = "Option2",
              Value = "2"
          }
      }))
<script>
    function change() {
        var value = $("#Choice").val();
    }
</script>

....

columns.Bound(p=> p.FirsName);
columns.Bound(p => p.LastName);
//How to Bind Choice???

I also need a text ("Option1", or "Option2") in BackEnd Code. Any solutions?

Edited

I did exactly what they said:

View:

 columns.Bound(p => p.Choice).ClientTemplate("#=Choice#");

Controller:

public ActionResult Index()
    {
        PopulateCategories();
        return View();
    }
    

.....

 private void PopulateCategories()
        {
            var dataContext = new TestDB();
            var categories = dataContext.Peoples
                .Select(c => new People()
                {
                    ChoiceID = c.ChoiceID,
                    Choice = c.Choice
                })
                .OrderBy(e => e.Choice);
            ViewData["categories"] = categories;
            ViewData["defaultCategory"] = categories.First();
        }       
    

But this doesn't work...

You just need tp take look at the documentation they provide : http://demos.kendoui.com/web/grid/editing-custom.html

It is a custom grid so there MAY also be some JS changes involved also. You are binding from your model, and the kendo.js will look after the rest.

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ClientProductViewModel>()
.Name("grid")
.Columns(columns =>
{
    columns.Bound(p => p.ProductName);
    columns.Bound(p => p.Category).ClientTemplate("#=Category.CategoryName#").Width(160);
    columns.Bound(p => p.UnitPrice).Width(120);
    columns.Command(command => command.Destroy()).Width(90);
})
.ToolBar(toolBar =>
    {
        toolBar.Create();
        toolBar.Save();
    })
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
    .Ajax()
    .Batch(true)
    .ServerOperation(false)
    .Events(events => events.Error("error_handler"))
    .Model(model =>
    {
        model.Id(p => p.ProductID);
        model.Field(p => p.ProductID).Editable(false);
        model.Field(p => p.Category).DefaultValue(
            ViewData["defaultCategory"] as Kendo.Mvc.Examples.Models.ClientCategoryViewModel);
    })
    .PageSize(20)
    .Read(read => read.Action("EditingCustom_Read", "Grid"))
    .Create(create => create.Action("EditingCustom_Create", "Grid"))
    .Update(update => update.Action("EditingCustom_Update", "Grid"))        
    .Destroy(destroy => destroy.Action("EditingCustom_Destroy", "Grid"))
)
)

Your csHTML file needs to bind the input and passback on the trigger, whichever you are doing.

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