2012-06-20 16 views
9

私はMVC3のKendoUI MVCで作業しています。剣道UI MVCのグリッドにドロップダウンリストの値を設定して取得するにはどうすればよいですか?

グリッド列のドロップダウンを取得できました。しかし、私は選択された値をどのように設定するかについての手がかりがなく、私が選択した値を保存しないときにそれを保存すると、

グリッド

@using Perseus.Areas.Communication.Models 
@using Perseus.Common.BusinessEntities; 


<div class="gridWrapper"> 
    @(Html.Kendo().Grid<CommunicationModel>() 
     .Name("grid") 
     .Columns(colums => 
     { 
      colums.Bound(o => o.communication_type_id) 
       .EditorTemplateName("_communicationDropDown") 
       .ClientTemplate("#: communication_type #") 
       .Title("Type") 
       .Width(180); 
      colums.Bound(o => o.sequence).Width(180); 
      colums.Bound(o => o.remarks); 
      colums.Command(command => command.Edit()).Width(50); 
     }) 
     .Pageable() 
     .Sortable() 
     .Filterable() 
     .Groupable() 
     .Editable(edit => edit.Mode(GridEditMode.InLine)) 
     .DataSource(dataSource => dataSource 
      .Ajax() 
      .ServerOperation(false) 
      .Model(model => model.Id(o => o.communication_id)) 
       .Read(read => read.Action("AjaxBinding", "Communication", new { id = @ViewBag.addressId })) 
       .Update(update => update.Action("Update", "Communication")) 
      .Sort(sort => { sort.Add(o => o.sequence).Ascending(); }) 
      .PageSize(20) 
     ) 
    ) 
</div> 

EditorTemplate「_communicationDropDown

@model Perseus.Areas.Communication.Models.CommunicationModel 


@(Html.Kendo().DropDownListFor(c => c.communication_type_id) 
     .Name("DropDownListCommunication") 
      .DataTextField("description1") 
      .DataValueField("communication_type_id") 
      .BindTo(ViewBag.CommunicationTypes)) 
+1

y解?私は同じ問題があります。ドロップダウンリストにテキストを表示することができます。どのように値を設定できますか。 –

+0

問題が解決されたようですが、「communication_type」フィールドは何ですか?あなたのclientTemplateで? –

+0

'.ClientTemplate("#:communication_type# ")'をどこで定義していますか?グリッド全体のコントローラーをポストしてください。 –

答えて

18

私は、これは指摘する重要なものは、DropDownListの名が列名の属性と一致しなければならないことであると思います。HTML属性名= ""、列の見出しではありません。デフォルトのエディタコントロールをエディタテンプレートからの別のコントロールに置き換えて編集操作中に置き換えるため、名前属性が一致する必要があります。合わないDOMが更新操作のためにモデルにシリアル化されると、エディタテンプレートコントロールからの値は無視されます。デフォルトでは、マークアップで上書きされない限り、モデルクラスに表示されるプロパティ変数名です。

(回答はレコードの挿入操作を含むように編集されました)ここで

は実施例である:

モデルクラス:

public class Employee 
{ 
    public int EmployeeId { get; set; } 
    public string Name { get; set; } 
    public string Department { get; set; } 
} 

ビュー:そのEditorTemplatesフォルダにある

@(Html.Kendo().Grid<Employee>() 
    .Name("Grid") 
    .Columns(columns => 
    { 
     columns.Bound(p => p.Name).Width(50); 
     columns.Bound(p => p.Department).Width(50).EditorTemplateName("DepartmentDropDownList"); 
     columns.Command(command => command.Edit()).Width(50); 
    }) 
    .ToolBar(commands => commands.Create()) 
    .Editable(editable => editable.Mode(GridEditMode.InLine)) 
    .DataSource(dataSource => dataSource 
     .Ajax() 
     .Model(model => model.Id(p => p.EmployeeId)) 
     .Read(read => read.Action("GetEmployees", "Home")) 
     .Update(update => update.Action("UpdateEmployees", "Home")) 
     .Create(create => create.Action("CreateEmployee", "Home")) 
    ) 
) 

パーシャルビューエディタテンプレート、ファイル名 "DepartmentDropDownList"、このビューに固有です。すなわち、ホーム\ビュー\ EditorTemplates \ DepartmentDropDownList.cshtml読み取り操作のための

@model string 

@(Html.Kendo().DropDownList() 
    .Name("Department") //Important, must match the column's name 
    .Value(Model) 
    .SelectedIndex(0) 
    .BindTo(new string[] { "IT", "Sales", "Finance" })) //Static list of departments, can bind this to anything else. ie. the contents in the ViewBag 

コントローラー:更新操作のための

public ActionResult GetEmployees([DataSourceRequest]DataSourceRequest request) 
{ 
    List<Employee> list = new List<Employee>(); 
    Employee employee = new Employee() { EmployeeId = 1, Name = "John Smith", Department = "Sales" }; 
    list.Add(employee); 
    employee = new Employee() { EmployeeId = 2, Name = "Ted Teller", Department = "Finance" }; 
    list.Add(employee); 

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

コントローラー:作成操作のための

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult UpdateEmployees([DataSourceRequest] DataSourceRequest request, Employee employee) 
{ 
    return Json(new[] { employee }.ToDataSourceResult(request, ModelState)); 
} 

コントローラ:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult CreateEmployee([DataSourceRequest] DataSourceRequest request, Employee employee) 
{ 
    employee.EmployeeId = (new Random()).Next(1000); 
    return Json(new[] { employee }.ToDataSourceResult(request, ModelState)); 
} 
+1

* Facepalm *私はそれを考えなかったとは信じられません。ドロップダウンの名前を一致する列の名前に変更すると、魅力的に機能します!どうもありがとうございました! – bjornruysen

+0

EditorTemplateNameの良い例が見つかりました。あなたは "DepartmentDropDownList"とは何ですか?部分的なビューまたはアスキー、そしてどのフォルダに置くべきでしょうか? –

+1

を見つけました。現在のコントローラ(または一般的な)ViewフォルダのEditorTemplatesフォルダに配置する必要があります。 –

関連する問題