2012-06-20 22 views
9

Tôi đang làm việc với KendoUI MVC trong MVC3.Làm cách nào tôi có thể đặt và nhận giá trị của danh sách thả xuống trong lưới trong KEM UI UI MVC?

Tôi đã quản lý để có danh sách thả xuống trong cột lưới. Nhưng tôi không có đầu mối về cách thiết lập giá trị đã chọn, và khi tôi lưu nó không lưu giá trị đã chọn của tôi.

Lưới

@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> 

Các 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

Bạn có nhận được y giải pháp? Tôi có cùng một vấn đề. Tôi có thể hiển thị văn bản trong danh sách thả xuống, Làm cách nào tôi có thể đặt giá trị. –

+0

Dường như vấn đề được giải quyết, bạn có thể làm rõ trường '' communication_type '' là gì? trong clientTemplate của bạn? –

+0

Nơi bạn định nghĩa '.ClientTemplate (" #: communication_type # ")'? Bạn có thể vui lòng gửi bộ điều khiển của bạn của toàn bộ lưới điện. –

Trả lời

18

Tôi nghĩ rằng đây là một điều quan trọng để chỉ ra là tên DropDownList phải phù hợp với thuộc tính tên cột. Các html tên thuộc tính Các thuộc tính tên phải phù hợp với điều này để làm việc, vì bạn đang thay thế điều khiển trình soạn thảo mặc định bằng một điều khiển khác đến từ một mẫu trình soạn thảo để thay thế nó trong suốt quá trình chỉnh sửa. không khớp khi DOM được tuần tự hóa trở lại vào mô hình cho hoạt động cập nhật, giá trị từ điều khiển mẫu trình soạn thảo sẽ bị bỏ qua. Theo mặc định nó là tên biến thuộc tính xuất hiện trong lớp mô hình, trừ khi overriden trong đánh dấu lên.

(Trả lời chỉnh sửa để bao gồm thao tác ghi chèn).

Dưới đây là một ví dụ làm việc:

Mẫu Class:

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

Xem:

@(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")) 
    ) 
) 

Nhìn ra một khoảng biên tập mẫu, tên file "DepartmentDropDownList", nằm trong thư mục EditorTemplates rằng cụ thể cho chế độ xem này. I E. Trang chủ \ Views \ 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 

điều khiển cho các hoạt động đọc:

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)); 
} 

điều khiển cho các hoạt động Cập nhật:

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

điều khiển cho các hoạt động Tạo:

[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 * Tôi không thể tin rằng tôi không nghĩ về điều đó. Thay đổi tên của trình đơn thả xuống thành tên cột phù hợp hoạt động như một sự quyến rũ! Cảm ơn nhiều! – bjornruysen

+0

Không tìm thấy bất kỳ ví dụ hay về EditorTemplateName. Bạn có thể làm rõ "DepartmentDropDownList" là gì? một phần xem hoặc ascx, và trong thư mục nào tôi nên đặt nó? –

+1

Đã tìm ra. Cần đặt nó trong thư mục EditorTemplates cho thư mục View (hoặc common) của controller hiện tại. –

Các vấn đề liên quan