2011-12-24 40 views
0

在Index.cshtml我有以下幾點:試圖填充Telerik的網格,但網格一直顯示爲空

@{ 
    Html.Telerik().Grid<hekomaseru.Models.testdbEntities1>("testtable") 
     .Name("grid1") 
     .Pageable() 
     .Sortable() 
     .Filterable() 
     .Groupable() 
     .Render(); 
} 

而在HomeController.cs我這樣做:

public ActionResult Index() 
    { 
     List<int> abc = new List<int>() { 1, 2, 3, 4, 5 }; 
     ViewData["testtable"] = abc; 

     return View(); 
    } 

對於一些理由雖然當一切都加載網格總是空的(沒有記錄顯示)。任何想法,爲什麼它不會工作?

我有其他的東西Telerik的工作(即下拉菜單),所以我不認爲這有什麼與做專..

回答

2

網格需要綁定到對象列表。因爲它們是值類型,所以ints列表將不起作用。字符串列表將像Lester說的那樣工作,但唯一的屬性是長度。如果你想要一個int列表,你可以像這樣添加一個類。

public class Numbers 
{ 
    public Numbers(int number) 
    { 
    Num = number; 
    } 
    public int Num 
    { 
    get; 
    set; 
    } 
} 

然後在控制器中。

public ActionResult Index() 
{ 
    List<Numbers> abc = new List<Numbers>(); 
    abc.Add(new Numbers(1)); 
    abc.Add(new Numbers(2)); 
    abc.Add(new Numbers(3)); 
    abc.Add(new Numbers(4)); 
    abc.Add(new Numbers(5)); 

    return View(abc); 
} 

@model List<Numbers> 

@{ 
    Html.Telerik().Grid(Model) 
     .Name("grid1") 
     .Pageable() 
     .Sortable() 
     .Filterable() 
     .Groupable() 
     .Render(); 
} 

,使電網更加有趣,只需添加更多特性的數字類。

1

我只用了Telerik的MVC簡單的控制,但我從未像這樣綁定過ViewData。你可以將其更改爲:

@model IList<int> 

@{ 
    Html.Telerik().Grid(Model) 
     .Name("grid1") 
     .Pageable() 
     .Sortable() 
     .Filterable() 
     .Groupable() 
     .Render(); 
} 

和你的控制器:

public ActionResult Index() 
{ 
    List<int> abc = new List<int>() { 1, 2, 3, 4, 5 }; 

    return View(abc); 
} 

這幾乎恰好我如何綁定,所以它應該工作。

+1

當我這樣做時出現錯誤:編譯器錯誤消息:CS0452:'int'類型必須是引用類型才能在通用類型或方法'Telerik.Web.Mvc中將其用作參數'T' .UI.ViewComponentFactory.Grid (System.Collections.Generic.IEnumerable )' – tweetypi 2011-12-24 04:36:46

+0

看起來它不滿意int。嘗試將其更改爲字符串列表。 – Lester 2011-12-24 04:39:49

+0

剛剛做了,它現在可以工作,但它不顯示字符串內容,而是顯示每個字符串的長度(所以如果字符串是「abc」,它顯示'3'而不是「abc」,並且列標題是'length '):S – tweetypi 2011-12-24 04:50:13