2016-06-22 8 views
1

Ich muss einfache Dropdown-Liste implementieren, um vordefinierte Kulturen zu wählen.DropDownList in Kendo Grid in Asp.Net Core

Mein Gitter:

@(Html.Kendo().Grid<NewLibrary.ViewModels.BookViewModel>() 
    .Name("booksGrid") 
    .Columns(column => 
    { 
     column.Bound(m => m.Name); 
     column.Bound(m => m.Culture).EditorTemplateName("CultureSelectorTemplate"); 
    }) 
    .ToolBar(toolBar => 
    { 
     toolBar.Create(); 
     toolBar.Save(); 
    }) 
    .Sortable() 
    .Pageable(pageable => pageable 
     .Refresh(true) 
     .PageSizes(true) 
     .ButtonCount(10) 
    ) 
    .HtmlAttributes(new { style = "border-style: double; border-width: 5px" }) 
    .Editable(e => e.Mode(GridEditMode.InCell)) 
    .DataSource(dataSource => dataSource 
     .Ajax() 
     .PageSize(20) 
     .ServerOperation(false) 
     .Model(m => 
     { 
      m.Id(f => f.BookId); 
      m.Field(f => f.Name); 
      m.Field(f => f.Culture); 
     }) 
     .Create(create => create.Action("CreateBooks", "Books")) 
     .Read(read => read.Action("ReadBooks", "Books")) 
     .Update(update => update.Action("UpdateBooks", "Books")) 
     .Destroy(destroy => destroy.Action("DeleteBooks", "Books")) 
    ) 
) 

Mein Redakteur Vorlage in/Shared/EditorTemplates:

@(Html.Kendo().DropDownList() 
    .Name("Culture") 
    .DataTextField("Text") 
    .DataValueField("Value") 
    .BindTo(new List<SelectListItem>() 
    { 
     new SelectListItem() 
     { 
      Text = "English", 
      Value = "en" 
     }, 
     new SelectListItem() 
     { 
      Text = "Spanish", 
      Value = "es" 
     }, 
     new SelectListItem() 
     { 
      Text = "French", 
      Value = "fr" 
     } 
    }) 
) 

Meine Viewmodel:

public class BookViewModel 
{ 
    public string BookId { get; set; } 

    public string Name { get; set; } 

    public string Culture { get; set; } 
} 

Das Problem ist, dass ich kann nicht binden Werte von Dropdown-Liste zu Modell, wenn ich sie aus der Liste auswählen und dann ein anderes Buch bearbeiten, verschwindet der Wert aus der Liste.

Was ist das Problem mit dieser Implementierung, wie kann ich es richtig machen, während googlen Dutzende von gleichen Antworten, gab mir überhaupt nichts.

Also, was ist der richtige Weg, DropDownList in Kendo Grid über Asp.Net Core zu implementieren?

Antwort

0

Ok, wie es sein muss.

Meine Ansicht:

@(Html.Kendo().Grid<BookViewModel>() 
     .Name("booksGrid") 
     .Columns(column => 
     { 
      column.Bound(m => m.Name); 
      column.Bound(m => m.Culture).ClientTemplate("#=Culture.NativeName#").EditorTemplateName("CultureSelectorTemplate"); 
     }) 
     .ToolBar(toolBar => 
     { 
      toolBar.Create(); 
      toolBar.Save(); 
     }) 
     .Sortable() 
     .Pageable(pageable => pageable 
      .Refresh(true) 
      .PageSizes(true) 
      .ButtonCount(10) 
     ) 
     .HtmlAttributes(new { style = "border-style: double; border-width: 5px" }) 
     .Editable(e => e.Mode(GridEditMode.InCell)) 
     .DataSource(dataSource => dataSource 
      .Ajax() 
      .PageSize(20) 
      .ServerOperation(false) 
      .Model(m => 
      { 
       m.Id(f => f.BookId); 
       m.Field(f => f.Name); 
       m.Field(f => f.Culture).DefaultValue(ViewData["defaultCulture"] as CultureViewModel);//new SelectListItem() { Text = "English", Value = "en" }); 
      }) 
      .Create(create => create.Action("CreateBooks", "Books")) 
      .Read(read => read.Action("ReadBooks", "Books")) 
      .Update(update => update.Action("UpdateBooks", "Books")) 
      .Destroy(destroy => destroy.Action("DeleteBooks", "Books")) 
     ) 
     .Events(e => e.DataBound("onGridDataBound")) 
    ) 

Meine Viewmodels:

public class BookViewModel 
    { 
     public string BookId { get; set; } 

     public string Name { get; set; } 

     public CultureViewModel Culture { get; set; } 
    } 

public class CultureViewModel 
    { 
     public string NativeName { get; set; } 

     public string TwoLetterCode { get; set; } 
    } 

Mein Redakteur Vorlage:

@model CultureViewModel 
@(Html.Kendo().DropDownListFor(m => m) 
    .DataTextField("NativeName") 
    .DataValueField("TwoLetterCode") 
    .BindTo(new List<CultureViewModel>() 
    { 
     new CultureViewModel() 
     { 
      NativeName = "English", 
      TwoLetterCode = "en" 
     }, 
     new CultureViewModel() 
     { 
      NativeName = "Spanish", 
      TwoLetterCode = "es" 
     }, 
     new CultureViewModel() 
     { 
      NativeName = "French", 
      TwoLetterCode = "fr" 
     } 
    }) 
) 

Endlich Sie müssen Sie Standardwert in ViewData in Index-Methode bevölkern, oder im Raster DefaultValue direkt.