【问题标题】:Adding an EditorTemplate to a Kendo Grid将 EditorTemplate 添加到 Kendo Grid
【发布时间】:2013-07-30 17:19:00
【问题描述】:

我正在尝试将 EditorTemplate 添加到我的剑道网格中,我已经能够做到这一点。

我遇到的问题是,当我进入编辑模式时,下拉列表会自动转到列表中的第一个项目,而不是应该编辑的项目。

查看(索引.CSHTML):

@(Html.Kendo().Grid<DropDown.Models.MarkupState>()
              .Name("PayrollMarkupGrid")
              .Columns(columns =>
                       {
                           columns.Bound(p => p.PayrollMarkupId).Hidden(true);
                           columns.Bound(p => p.StateId).Hidden(true);
                           columns.Bound(p => p.State).Width(80);
                           columns.Bound(p => p.MaintenancePercentage).Title("Maint. %").Width(80);
                           columns.Bound(p => p.OfficePercentage).Title("Office %").Width(80);
                           columns.Command(command =>
                                           {
                                               command.Edit();
                                               command.Destroy();
                                           }).Title("Actions");
                       })
              .ToolBar(toolbar => toolbar.Create())
              .Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
              .DataSource(dataSource => dataSource
                                            .Ajax()
                                            .Model(model => model.Id(p => p.State))
                                            .Read(read => read.Action("Read", "Home"))
                                            .Create(create => create.Action("Create", "PayrollMarkup"))
                                            .Update(update => update.Action("Create", "PayrollMarkup"))
                                            .Destroy(destroy => destroy.Action("Delete", "PayrollMarkup"))
              ))

查看(StatesEditor.cshtml):

@model DropDown.Models.State
@{
    var shortName = "";
}
@if (Model != null)
{
    shortName = Model.ShortName;
}




@(Html.Kendo().DropDownList()
    .Name("State")
    .DataValueField("StateId")
    .DataTextField("ShortName")
    .Value(shortName)
    .BindTo((System.Collections.IEnumerable)ViewData["states"])
)

控制器(索引):

public ActionResult Index()
    {
        var stateList = new List<State>();

        var state1 = new State
        {
            StateId = 1,
            ShortName = "CA"
        };
        stateList.Add(state1);

        var state2 = new State
        {
            StateId = 2,
            ShortName = "CT"
        };
        stateList.Add(state2);

        var state3 = new State
        {
            StateId = 3,
            ShortName = "MA"
        };
        stateList.Add(state3);

        var state4 = new State
        {
            StateId = 4,
            ShortName = "RI"
        };
        stateList.Add(state4);

        var state5 = new State
        {
            StateId = 5,
            ShortName = "TX"
        };
        stateList.Add(state5);

        var state6 = new State
        {
            StateId = 6,
            ShortName = "SC"
        };
        stateList.Add(state6);

        ViewData["states"] = stateList;

        return View();
    }

控制器(读取):

public ActionResult Read([DataSourceRequest] DataSourceRequest request)
    {
        var payrollMarkupList = new List<MarkupState>();

        var markup1 = new MarkupState()
        {
            PayrollMarkupId = 1,
            StateId = 1,
            State = "CA",
            MaintenancePercentage = (decimal?)1.1
        };
        payrollMarkupList.Add(markup1);

        var markup2 = new MarkupState()
        {
            PayrollMarkupId = 2,
            StateId = 2,
            State = "CT",
            MaintenancePercentage = (decimal?)3.2
        };
        payrollMarkupList.Add(markup2);

        var markup3 = new MarkupState()
        {
            PayrollMarkupId = 3,
            StateId = 3,
            State = "MA",
            MaintenancePercentage = (decimal?)8.5
        };
        payrollMarkupList.Add(markup3);


        return Json(payrollMarkupList.ToDataSourceResult(request));
    }

模型(State.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DropDown.Models
{
    public class State
    {
        public int StateId { get; set; }
        public string ShortName { get; set; }
    }
}

模型(MarkupState.cs):

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace DropDown.Models
{
    public class MarkupState
    {
        public int PayrollMarkupId { get; set; }
        [UIHint("StatesEditor")]
        public string State { get; set; }
        public int StateId { get; set; }
        public decimal? MaintenancePercentage { get; set; }
        public decimal? OfficePercentage { get; set; }
    }
}

这是一个测试解决方案的链接,但我已经提供了上面的所有代码:

http://www.kendoui.com/clientsfiles/67b8816e-9aae-631b-85d4-ff000054ddc5_dropdown.zip?sfvrsn=0

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 kendo-ui kendo-grid


    【解决方案1】:

    设置StateId为可见列,并使用ClientTemplate显示“State”字段:

    columns.Bound(p => p.StateId).Width(80).ClientTemplate("#=State#");
    columns.Bound(p => p.State).Hidden(true);
    

    将自定义EditorTemplate设置为模型中的“StateId”字段:

    public class MarkupState
    {
        public int PayrollMarkupId { get; set; }
        public string State { get; set; }
        [UIHint("StatesEditor")]
        public int StateId { get; set; }
        public decimal? MaintenancePercentage { get; set; }
        public decimal? OfficePercentage { get; set; }
    }
    

    通过以下方式修改EditorTemplate:

    @model int?
    
    
    @(Html.Kendo().DropDownListFor(m => m)
        .DataValueField("StateId")
        .DataTextField("ShortName")
        .BindTo((System.Collections.IEnumerable)ViewData["states"])
    )
    

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多