8

Ben varlık çerçevesini kullandığım asp.net MVC 4 uygulamasını oluşturdum ve "Data" modeli modeldir.Asp.net MVC 4 Razor'da nasıl bağlanır MVP 4 Razor

AdventureWorksTrainingEntities _dbContext = new AdventureWorksTrainingEntities(); 
Data _data = new Data(); //Model 

Şimdi, tablonun verilerini kendo ızgarasına görüntülemek istiyorum. denetleyici olarak, ben aşağıdaki kodu kullanıyorum:

public ActionResult Index() 
     { 
      List<Movie> dataForGrid= _dbContext.Movies.ToList(); 
      return View(dataForGrid); 
     } 

Şimdi ben Kendo Grid verilerin görüntülenmesi için hiçbir fikrim yok, (i Kendo ve MVC için yeni).

@model IEnumerable<MvcApp.Models.Data> 
@using Kendo.Mvc.UI 
@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Grid For Data</h2> 
Html.Kendo().Grid<Order>() 
    .Name("Grid") 
    .DataSource(dataSource => dataSource // Not implemented 
) 

cevap

12

Nihayet got cevap::

Görünüm:

@(Html.Kendo().Grid<KendoUI.Models.EmployeeViewModel>() 
      .Name("Grid") 
      .Columns(columns => 
      { 
       columns.Bound(p => p.name).Title("Name"); 
       columns.Bound(p => p.gender).Title("Gender"); 
       columns.Bound(p => p.designation).Title("Designation").Width("300px"); 
       columns.Bound(p => p.department).Title("Department").Width("300px"); 
      }) 

      .Editable(editable => editable.Mode(GridEditMode.InLine)) 
      .Navigatable() 
      .Pageable() 
      .Sortable() 
      .Scrollable() 
      .DataSource(dataSource => dataSource // Configure the grid data source 
      .Ajax() 
      .Model(model => 
      { 
       model.Id(x => x.id); 
      }) 
       .Read(read => read.Action("Employee_Read", "Home")) // Set the action method which will return the data in JSON format 
      ) 
      ) 

Denetleyici:

public ActionResult Employee_Read([DataSourceRequest]DataSourceRequest request) 
     { 
      IQueryable<Bhupendra_employees> Details = _dbContext.Bhupendra_employees; 
      DataSourceResult result = Details.ToDataSourceResult(request, p => new EmployeeViewModel 
        { 
         id = p.id, 
         name = p.name, 
         gender = p.gender, 
         designation = p.designation, 
         department = p.Bhupendra_Dept.Dept_Description 
        }); 
      return Json(result, JsonRequestBehavior.AllowGet); 
     } 

Modeli:

public class EmployeeViewModel 
    { 
     public Int32 id { get; set; } 
     public String name { get; set; } 
     public String gender { get; set; } 
     public String designation { get; set; } 
     public String department { get; set; } 
     //public DateTime dob { get; set; } 
    } 
şunlarla ama çalışmıyor denedim
+0

Bu durum jquery başlatma işleminde de geçerlidir? – Samra

4

Denetleyiciniz adı veriler daha sonra aşağıdaki

Modelinizi kullanabilirsiniz olduğunda bulunduğunuz Görünüm Sadece izleyerek Modeli

kimliği adı verilen Anahtarı sahip olduğunu varsayarak eklemem gerekiyor
public ActionResult ReadDegrees([DataSourceRequest] DataSourceRequest request) 
    { 
     ViewBag.Countries = CommonController.CountryList(); 
     ViewBag.DegreeTypes = CommonController.DegreeTypeList(); 
     using (var _dbContext= new AdventureWorksTrainingEntities()) 
     { 
      return Json(_dbContext.Movies.ToList.ToDataSourceResult(request)); 
     } 
    } 

Html.Kendo().Grid<Order>() 
.Name("Grid") 
.DataSource(dataSource => dataSource 
    .Ajax() 
    .PageSize(20) 
    .Model(model => model.Id(p => p.ID)) 
    .Read(read => read.Action("ReadDegrees", "Data")))) 
+0

'.Read (read => read.Action (" Index "," Data ")))) 'index' yerine 'ReadDegrees' olmalıdır? – Steve

+0

evet haklısınız – Monah

+0

'Html.KendoGrid'de hata alıyorum' Htmlhelper 'Kendo' tanımını içermez .. ama hem View hem de web.config içinde 'Kendo.MVC.dll 'referans ekledim Proje ayrıca. Hatayı nasıl kaldıracağınızı biliyor muydunuz? – Steve