2014-10-16 4 views
1

Bend Kendo Grid를 원격 데이터에 바인딩하면 데이터가있는 일반 텍스트 만 다시 표시되지만 채워진 Grid로 View를 반환하고 싶습니다. Kendo Grid의 데이터로 View를 반환하려면 어떻게해야합니까?Kendo Grid를 원격 데이터에 바인딩 MVC 4

컨트롤러 :

public ActionResult Index([Bind(Prefix = "Id")] int categoryId,[DataSourceRequest]DataSourceRequest request) 
    { 
     var category = _db.Categories.Find(categoryId); 

     var model = 
      db.Query<Entry>() 
       .OrderBy(en => en.Id) 
       .Where(en => en.CategoryId == categoryId) 
       .Select(en => new EntryViewModel 
       { 
        Id = en.Id, 
        CategoryId = en.CategoryId, 
        Title = en.Title, 
        Username = en.Username, 
        Password = en.Password, 
        Url = en.Url, 
        Description = en.Description, 
       }).AsEnumerable(); 

     return Json(model.ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 
    } 

보기 :

@model IEnumerable<PasswordCloudApp.ViewModels.EntryViewModel> 


@(Html.Kendo().Grid<PasswordCloudApp.ViewModels.EntryViewModel>() 
.Name("grid") 
.Columns(columns => 
{ 
    columns.Bound(p => p.Title); 
    columns.Bound(p => p.Username).Width(100); 
    columns.Bound(p => p.Password); 
    columns.Bound(p => p.Url); 
}) 
.Pageable() 
.Sortable() 
.Scrollable() 
.Filterable() 
.HtmlAttributes(new { style = "height:430px;" }) 
.DataSource(dataSource => dataSource 
    .Ajax() 
    .PageSize(20) 
        .Read(read => read.Action("Index", "Entry")) 
) 
) 

어떤 제안이?

+0

코드가 누락 된 경우를 제외하고 코드가 올바르게 표시됩니다. Kendo UI 또는 JQuery 스크립트가 누락되었을 수 있습니다. Kendo가 날짜 선택기 또는 무엇인가를 사용하여 성공적으로 설치되었음을 확인 했습니까? – CSharper

답변

1

다음과 같이 시도해보십시오. 그것은 당신이 필요로하는 것과 정확하게 일치하지 않을 것이지만, 요점은 인덱스 동작과 그리드 데이터를 검색하는 데 사용되는 동작을 구분하는 것입니다.

public ActionResult Index() 
{ 
    // Retrieve the viewmodel for the view here, depending on your data structure. 
    return View(new EntryViewModel); 
} 

public ActionResult GetData(int categoryId, [DataSourceRequest]DataSourceRequest request) 
{ 
    var category = _db.Categories.Find(categoryId); 
    var model = db.Query<Entry>() 
     .OrderBy(en => en.Id) 
     .Where(en => en.CategoryId == categoryId) 
     .Select(en => new GridViewModel 
     { 
      Id = en.Id, 
      CategoryId = en.CategoryId, 
      Title = en.Title, 
      Username = en.Username, 
      Password = en.Password, 
      Url = en.Url, 
      Description = en.Description, 
     }).AsEnumerable(); 

    return Json(model.ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 
} 

@model IEnumerable<PasswordCloudApp.ViewModels.EntryViewModel> 
@(Html.Kendo().Grid<PasswordCloudApp.ViewModels.GridViewModel>() 
    .Name("grid") 
    .Columns(columns => 
    { 
     columns.Bound(p => p.Title); 
     columns.Bound(p => p.Username).Width(100); 
     columns.Bound(p => p.Password); 
     columns.Bound(p => p.Url); 
    }) 
    .Pageable() 
    .Sortable() 
    .Scrollable() 
    .Filterable() 
    .HtmlAttributes(new { style = "height:430px;" }) 
    .DataSource(dataSource => dataSource 
     .Ajax() 
     .PageSize(20) 
     .Read(read => read.Action("GetData", "Entry", new { categoryId = Model.CategoryID }))) 
) 
+0

저에게 효과적입니다. 도움을 많이 주셔서 감사합니다. – McKeymayker

관련 문제