2016-10-04 7 views
1

私はこのようなビューモデルを持っています。このモデルをgridviewにバインドし、各行に対してMatchingCustomersリストからドロップダウンを作成する必要があります。ここで剣道のグリッド内のオブジェクトのリストからドロップダウンを作成

public class CustomerMappingViewModel 
{ 
    public CustomerViewModel Customer; 
    public IEnumerable<MatchingCustomer> MatchingCustomers; 
} 

public class CustomerViewModel 
{ 
    public int CustomerId {get;set;} 
    public string Name {get;set;} 
    public string Address {get;set;} 
    public string City {get;set;} 
    public string State {get;set;} 
    public string PhoneNumber {get;set;} 
} 

public class MatchingCustomer 
{ 
    public string Name {get;set;} 
    public string Address {get;set;} 
    public string City {get;set;} 
    public string PhoneNumber {get;set;} 

    public string CustomerInfo 
    { 
     get { return Name + " - " + AdditionalName + " " + Address + ", " + City + ", " + State + " " + Zipcode + ", " + PhoneNumber; } 
    } 
} 

がグリッド

@(Html.Kendo().Grid<CustomerMappingViewModel>() 
          .Name("CustomerGrid") 
          .Columns(columns => 
          { 
           columns.Bound(c =>  c.Customer.Name).Title("Name"); 
           columns.Bound(c => c.Customer.Address).Title("Address"); 
           columns.Bound(c => c.Customer.City).Title("City"); 
           **//I NEED A COLUMN HERE to bind to MatchingCustomers list as a dropdown with CustomerInfo as text and name as value but not sure how.**        

          }) 
          .Pageable() 
          .Sortable() 
          .Filterable() 
          .Scrollable(s => s.Height("auto")) 
          .DataSource(dataSource => dataSource 
           .Ajax() 
           .PageSize(50) 
           .Events(events => events.RequestEnd("onRequestEnd")) 
                   .Model(model => model.Id(c => c.Customer.CustomerID)) 
                 .Read(read => read.Action("GetCustomers", "Customers")) 
        ) 
       ) 

ための図であり、すべてのヘルプは理解されるであろう。

答えて

0

次のコードスニペットを試してください。

ビュー

@(Html.Kendo().Grid<WebApplication2.Models.CustomerMappingViewModel>() 
          .Name("CustomerGrid") 
          .Columns(columns => 
          { 
          columns.Bound(c => c.Customer.Name).Title("Name"); 
          columns.Template(@<text></text>).ClientTemplate((@Html.Kendo().DropDownList() 
            .AutoBind(false) 
            .Name("ddl_1") 
             .DataTextField("City") 
             .DataValueField("Address") 
             .ToClientTemplate()).ToHtmlString()); 
          }) 
          .Pageable() 
          .Events(events => events 

      .DataBound("onDataBound") 

     ) 
          .Sortable() 
          .Filterable() 
          .Scrollable(s => s.Height("auto")) 
          .DataSource(dataSource => dataSource 
           .Ajax() 
           .PageSize(50) 
          .Model(model => model.Id(c => c.Customer.CustomerId)) 
          .Read(read => read.Action("GetCustomers", "Home")) 
) 
) 
<script> 
    function onDataBound(arg) { 
     var grid = $("#CustomerGrid").data("kendoGrid"); 
     var dataSource = grid.dataSource; 
     var allData = dataSource.data(); 
     for (var i = 0; i < allData.length; i++) { 
      var item = allData[i]; 
      var row = grid.tbody.find("tr[data-uid='" + item.uid + "']"); 
      $(row).find('[name="ddl_1"]').kendoDropDownList({ 
       dataTextField: "City", 
       dataValueField: "Address", 
       dataSource: item.MatchingCustomers, 
      }); 
     } 
    } 

</script> 

コントローラ

public ActionResult GetCustomers([DataSourceRequest] DataSourceRequest request) 
{ 
    List<CustomerMappingViewModel> lst = new List<CustomerMappingViewModel>(); 
    List<MatchingCustomer> lst1 = new List<MatchingCustomer>(); 
    lst1.Add(new MatchingCustomer() { City = "city1", Address = "add1" }); 
    lst1.Add(new MatchingCustomer() { City = "city2", Address = "add2" }); 
    lst.Add(new CustomerMappingViewModel() { Customer = new CustomerViewModel() { Name = "Jayesh1", CustomerId = 1 }, MatchingCustomers = lst1 }); 
    lst1 = new List<MatchingCustomer>(); 
    lst1.Add(new MatchingCustomer() { City = "city11", Address = "add11" }); 
    lst1.Add(new MatchingCustomer() { City = "city22", Address = "add22" }); 
    lst.Add(new CustomerMappingViewModel() { Customer = new CustomerViewModel() { Name = "Jayesh11", CustomerId = 12 }, MatchingCustomers = lst1 }); 

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

任意の心配なら、私に教えてください。

+0

これは機能します。どうもありがとうございます! – CoolDev

関連する問題