C# 试图填充telerik网格,但网格始终显示为空
在Index.cshtml中,我有以下内容:C# 试图填充telerik网格,但网格始终显示为空,c#,telerik,telerik-grid,C#,Telerik,Telerik Grid,在Index.cshtml中,我有以下内容: @{ Html.Telerik().Grid<hekomaseru.Models.testdbEntities1>("testtable") .Name("grid1") .Pageable() .Sortable() .Filterable() .Groupable() .Render(); } @{ Html.Telerik().
@{
Html.Telerik().Grid<hekomaseru.Models.testdbEntities1>("testtable")
.Name("grid1")
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.Render();
}
@{
Html.Telerik().Grid(“测试表”)
.名称(“grid1”)
.Pageable()
.Sortable()
.可过滤()
.Groupable()
.Render();
}
在HomeController.cs中,我执行以下操作:
public ActionResult Index()
{
List<int> abc = new List<int>() { 1, 2, 3, 4, 5 };
ViewData["testtable"] = abc;
return View();
}
public ActionResult Index()
{
列表abc=新列表(){1,2,3,4,5};
ViewData[“testtable”]=abc;
返回视图();
}
但由于某些原因,当所有内容都加载时,网格总是空的(没有要显示的记录)。你知道为什么它不起作用吗
我还有其他telerik功能(即下拉菜单),因此我认为这与此无关。我只简单地使用了telerik MVC控件,但我从未绑定到类似的ViewData。你能把它改成:
@model IList<int>
@{
Html.Telerik().Grid(Model)
.Name("grid1")
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.Render();
}
@model-IList
@{
Html.Telerik().Grid(模型)
.名称(“grid1”)
.Pageable()
.Sortable()
.可过滤()
.Groupable()
.Render();
}
和您的控制器:
public ActionResult Index()
{
List<int> abc = new List<int>() { 1, 2, 3, 4, 5 };
return View(abc);
}
public ActionResult Index()
{
列表abc=新列表(){1,2,3,4,5};
返回视图(abc);
}
这几乎正是我绑定的方式,所以它应该可以工作。我只简单地使用了Telerik MVC控件,但我从未绑定过这样的ViewData。你能把它改成:
@model IList<int>
@{
Html.Telerik().Grid(Model)
.Name("grid1")
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.Render();
}
@model-IList
@{
Html.Telerik().Grid(模型)
.名称(“grid1”)
.Pageable()
.Sortable()
.可过滤()
.Groupable()
.Render();
}
和您的控制器:
public ActionResult Index()
{
List<int> abc = new List<int>() { 1, 2, 3, 4, 5 };
return View(abc);
}
public ActionResult Index()
{
列表abc=新列表(){1,2,3,4,5};
返回视图(abc);
}
这几乎就是我绑定的方式,所以它应该工作。网格需要绑定到对象列表。整数列表不起作用,因为它们是值类型。字符串列表将像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);
}
public ActionResult Index()
{
列表abc=新列表();
abc.增加(新编号(1));
abc.增加(新编号(2));
abc.增加(新编号(3));
abc.增加(新编号(4));
abc.增加(新数字(5));
返回视图(abc);
}
景色
@model List<Numbers>
@{
Html.Telerik().Grid(Model)
.Name("grid1")
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.Render();
}
@型号列表
@{
Html.Telerik().Grid(模型)
.名称(“grid1”)
.Pageable()
.Sortable()
.可过滤()
.Groupable()
.Render();
}
要使网格更有趣,只需向Numbers类添加更多属性。网格需要绑定到对象列表。整数列表不起作用,因为它们是值类型。字符串列表将像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);
}
public ActionResult Index()
{
列表abc=新列表();
abc.增加(新编号(1));
abc.增加(新编号(2));
abc.增加(新编号(3));
abc.增加(新编号(4));
abc.增加(新数字(5));
返回视图(abc);
}
景色
@model List<Numbers>
@{
Html.Telerik().Grid(Model)
.Name("grid1")
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.Render();
}
@型号列表
@{
Html.Telerik().Grid(模型)
.名称(“grid1”)
.Pageable()
.Sortable()
.可过滤()
.Groupable()
.Render();
}
为了使网格更有趣,只需向Numbers类添加更多属性。这样做时,我会遇到一个错误:编译器错误消息:CS0452:类型“int”必须是引用类型,才能在泛型类型或方法“Telerik.Web.Mvc.UI.ViewComponentFactory.grid(System.Collections.generic.IEnumerable)中用作参数“T”'看起来对int不满意。请尝试将其更改为字符串列表。刚刚更改,它现在可以工作,但不显示字符串内容,而是显示每个字符串的长度(因此,如果字符串为“abc”,则显示“3”,而不是“abc”,列标题为“length”):I执行此操作时出错:编译器错误消息:CS0452:类型“int”必须是引用类型,才能将其用作泛型类型或方法“Telerik.Web.Mvc.UI.ViewComponentFactory.Grid(System.Collections.generic.IEnumerable)”中的参数“T”。看起来它对int不满意。请尝试将其更改为字符串列表。确实如此,它现在可以工作,但它不显示字符串内容,而是显示每个字符串的长度(因此,如果字符串为“abc”,则显示“3”而不是“abc”,列标题为“length”):s