Grid中的Telerik + ASP.NET MVC DropdownList

时间:2013-09-09 20:58:31

标签: c# asp.net asp.net-mvc telerik-grid telerik-mvc

我无法让DropDownList显示在Telerik MVC Grid的单元格中。我已经按照一个工作正常的教程项目,我看不出为什么它不起作用的任何差异。一个警告是这是一个区域而教程不是。有什么想法吗?

模型(位于单独的项目中 - \ External \ KPI \ KPI.Business.Entities \ Kpi.cs):

public sealed class Kpi
{
    #region Properties

    /// <summary>
    /// Gets or sets the KpiId.
    /// </summary>
    /// <value>The KpiId.</value>
    public long KpiId { get; set; }

    /// <summary>
    /// Gets or sets the Organization Id.
    /// </summary>
    /// <value>The Organization Id.</value>
    public string OrganizationId { get; set; }

    /// <summary>
    /// Gets or sets the channel.
    /// </summary>
    /// <value>
    /// The channel.
    /// </value>
    [UIHint("Channel"), Required]
    public string Channel { get; set; }
}

控制器(位于UI项目中 - \ Areas \ KPI \ Controllers \ KpiController.cs):

    public ActionResult Index(string organizationId)
    {
        if (!string.IsNullOrEmpty(organizationId))
        {
            initializeEditMode(organizationId);
            ViewBag.CustomerOrg = new OrganizationLogic(organizationId, Session["connectionString"].ToString()).GetName();
            var channels = new OrganizationLogic(organizationId, Session["connectionString"].ToString()).GetOrganizations();
            ViewBag.Channels = channels.Select(x => x.Name).ToArray();
        }

        return View();
    }

部分视图(位于UI项目中 - \ Areas \ KPI \ Views \ Kpi \ Editor Templates \ Channel.cshtml):

@using System.Collections
@using Telerik.Web.Mvc.UI
@model string

@(Html.Telerik().DropDownList()        
    .Name("Channel")
    .BindTo(new SelectList((IEnumerable)ViewData["channels"], "OrganizationId", "Name"))
)

查看(位于UI项目 - \ Areas \ KPI \ Views \ Kpi \ Index.cshtml):

@using KPI.Business.Entities
@using Telerik.Web.Mvc.UI
@model IList<KPI.Business.Entities.Kpi>
@{
    ViewBag.Title = "KPI";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    @ViewBag.CustomerOrg
</h2>
<div>
    @(Html.Telerik().Grid<Kpi>()
        .Name("kpiGrid")
        .ToolBar(commands => commands
            .Insert()
            .ButtonType(GridButtonType.ImageAndText)
            .ImageHtmlAttributes(new { style = "margin-left:0" })
            .Text("Insert New KPI"))
        .DataKeys(keys => keys.Add(v => v.KpiId))
        .Columns(columns =>
        {
            columns.Bound(kpi => kpi.KpiId).Hidden(true);
            columns.Bound(kpi => kpi.OrganizationId).Hidden(true);
            columns.Bound(kpi => kpi.Channel);
            columns.Bound(kpi => kpi.Name).Column.Title = "KPI Name";
            columns.Bound(kpi => kpi.Value);
            columns.Bound(kpi => kpi.StartDate).Width(150).Format("{0:d}");
            columns.Bound(kpi => kpi.EndDate).Width(150).Format("{0:d}");
            columns.Command(commands =>    commands.Edit().ButtonType(GridButtonType.Image)).Width(40);
        })
        .DataBinding(binding => binding
            .Ajax()
            .Select("KpiEditTemplate", "Kpi")
            .Insert("InsertKpi", "Kpi")
            .Update("UpdateKpi", "Kpi"))
        .ClientEvents(events =>     events.OnRowDataBound("Grid_onRowDataBound").OnSave("Grid_onSave").OnError("checkForSessionTimeout"))
        .Pageable()
        .Sortable()
        .Filterable()
        .Selectable()
    )
    @(Html.Telerik().Window()
        .Name("Window")
        .Visible(false)
        .Width(300)
        .Height(200)
        .Modal(true)
    )
</div>

2 个答案:

答案 0 :(得分:3)

Telerik文档不是最新的,因此它不会告诉您可以使用更简单的语法。我已经成功完成了这样的事情。无需UIHint属性。

@(Html.Telerik().Grid<MyViewModel>()
  .Name("Grid")
  .Columns(cols =>
    {
      cols.Bound(o => o.Property1);
      cols.Bound(o => o.Property2)
          .EditorTemplateName("Property2Template"); // this your .cshtml filename
    })
  .DataBinding(binding =>
    {
       //etc
    })
  // remainder of grid code
)

您的编辑器模板应位于您所在区域的Views / Shared / EditorTemplates目录中。希望有所帮助。

答案 1 :(得分:0)

据我所知,即使你有一个区域,编辑模板也需要相对于_Layout.cshtml。我确定在某个地方设置了这个设置,我只是没有看到它。我从以前的工程师那里继承了这个应用程序,移动该文件让我能够使用它。