2016-11-08 42 views
0

我在Kendo UI上使用MVC应用程序。我们有一个网格,当用户打开行进行编辑时,我们有一个保存公司名称的dropDownList。我试图让DDL默认为与该行相关的公司名称。Kendo UI DropDownListFor Set SelectedValue

这里的列代码:

columns.Bound(e => e.company_business_name).Width(220).Title("Company") 
    .EditorTemplateName("CompanyName"); 

和这里的editorTemplate代码:

@model string 

@(Html.Kendo().DropDownListFor(m => m) 
     .DataTextField("Text") 
     .DataValueField("Value") 
     .BindTo((System.Collections.IEnumerable)ViewData["Companies"]) 
) 

和填充的DDL的方法:

private void PopulateCompanies() 
    { 
     var companyList = new List<SelectListItem>(); 

     if (!string.IsNullOrEmpty(Session["Companies"] as string)) 
     { 
      companyList = (List<SelectListItem>)Session["Companies"]; 
     } 
     else 
     { 
      companyList = new DataAccess().GetCompanies(CurrentSettings.getUser().userId); 
      CacheCompanies(companyList); 
     } 

     ViewData["Companies"] = companyList; 
    } 

编辑:

更新了码。该DDL仍然填充,但我仍然没有得到选定的值,当我点击网格行上的“编辑”。感觉就像我在这里,帮助!

+0

我建议通过查看自定义编辑器演示开始(http://demos.telerik.com/aspnet-mvc/grid/editing-自定义)和Foreignkey列演示(http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn)。请注意,为了查看正在发生的* everything *,您必须打开随Kendo MVC一起安装的演示项目,因为在线MVC演示不会显示每个感兴趣的文件,例如custome编辑器演示,它不会显示EditorTemplate,演示项目应该在这里找到: \ wrappers \ aspnetmvc \示例 –

+0

GetCompanies返回什么?你能发布样本数据吗? – ataravati

回答

1

问题是,您的编辑器模板的模型是您的整个模型,而不是company_business_name属性(顺便提一下,您需要遵循标准的命名约定)。

你甚至不需要填写下拉列表。

你的模板编辑器应该是这样的:

@model string 

@(Html.Kendo().DropDownListFor(m => m) 
    .DataTextField("Text") 
    .DataValueField("Value") 
    .DataSource(x => 
     x.Read(read => read.Action("GetCompanies", "AddEntry")) 
    ) 
) 
+0

感谢ataravati,我修改了代码以反映您的建议。 DDL填充但仍未获取选定的值。 而你正在向合唱团传授关于命名约定,但我继承了这个小怪物。 – TrevorGoodchild

+0

@TrevorGoodchild在你的DDL中,尝试设置'.ValuePrimitive(true)',看看是否有效。 – ataravati