2016-11-08 96 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