Telerik MVC Grid不显示数据

时间:2011-10-05 14:41:08

标签: telerik-grid telerik-mvc

我正在使用版本2011.2.914的控件。每当我尝试将数据从我的控制器绑定到视图时,我无法将集合中的数据显示在网格上。我从网格中收到“无记录显示”消息。

我想指出,如果我使用Telerik网格,数据就会很好(见下文):  没有Telerik Grid的控制器

IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
                     return View("Index", customerList);

查看

@model IEnumerable<YeagerTech.YeagerTechWcfService.Customer>

@{
    ViewBag.Title = "Customer Index";
}
<h2>Customer Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Email
        </th>
        <th>
            Company
        </th>
        <th>
            FirstName
        </th>
        <th>
            LastName
        </th>
        <th>
            Address1
        </th>
        <th>
            Address2
        </th>
        <th>
            City
        </th>
        <th>
            State
        </th>
        <th>
            Zip
        </th>
        <th>
            HomePhone
        </th>
        <th>
            CellPhone
        </th>
        <th>
            Website
        </th>
        <th>
            IMAddress
        </th>
        <th>
            CreatedDate
        </th>
        <th>
            UpdatedDate
        </th>
        <th>
        </th>
    </tr>
    @if (ViewData.Model != null)
    {
        foreach (var item in Model)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Company)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address2)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.State)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Zip)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.HomePhone)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CellPhone)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Website)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IMAddress)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreatedDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UpdatedDate)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.CustomerID }) |
                @Html.ActionLink("Details", "Details", new { id = item.CustomerID })
            </td>
        </tr>
        }
    }
</table>

我尝试过ServerSide和Ajax Binding。两者都不起作用。我搜索了Telerik网站和网络,但无法找到遗漏的内容。

我需要做些什么才能解决此问题并获取网格中的数据?

以下是我的Customer控制器中用于Ajax绑定的代码(这是我真正想要的)。返回获取数据后,我有一个我保存的图像文件,其中数据位于customerList集合中( 没有选项可以附加StackOverflow中的任何文件 ,所以我无法告诉你。)

在渲染视图时,我在调试多个对象时捕获数据。 在绑定期间在网格中循环时,我在另一个文件中捕获了数据。

使用Telerik Grid的控制器:     使用Telerik.Web.Mvc;
    public class CustomerController:Controller          {

[GridAction]
         public ActionResult Index()
         {
                     IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
                     return View(new GridModel<YeagerTechWcfService.Customer>
                     {
                         Data = customerList
                     });

         }
 }

下面是相关视图中的代码。顺便说一句,所有绑定列都有智能感知,弹出我的模型中的列。这适用于Customer控制器中的Index操作。

@model Telerik.Web.Mvc.GridModel<YeagerTech.YeagerTechWcfService.Customer>
 @{
     ViewBag.Title = "Customer Index";
 }
 <h2>
     Customer Index</h2>
 <p>
     @Html.ActionLink("Create New", "Create")
 </p>
 <table>
     @(Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>()
   .Name("Customers")
   .Columns(columns =>
 {
     columns.Bound(o => o.Email);
     columns.Bound(o => o.Company);
     columns.Bound(o => o.FirstName);
     columns.Bound(o => o.LastName);
     columns.Bound(o => o.Address1);
     columns.Bound(o => o.Address2);
     columns.Bound(o => o.City);
     columns.Bound(o => o.State);
     columns.Bound(o => o.Zip);
     columns.Bound(o => o.HomePhone);
     columns.Bound(o => o.CellPhone);
     columns.Bound(o => o.Website);
     columns.Bound(o => o.IMAddress);
     columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}");
     columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}");
 }).DataBinding(dataBinding => dataBinding.Ajax().Select("Index", "Customer"))
 .Pageable()
 .Sortable()
 .Scrollable()
 .Resizable(resizing => resizing.Columns(true))
 .Filterable())
 </table>

1 个答案:

答案 0 :(得分:2)

可能与您的Index操作方法存在冲突,因为您有一个引用它的View。第一个测试是在不使用Ajax或其他任何东西的情况下显示你的网格。

在构建View Index时尝试初始化网格。请注意我在Grid构造函数中使用的 Model.Data

@(Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>(Model.Data)
   .Name("Customers")
   .Columns(columns =>
   {
       columns.Bound(o => o.Email);
       columns.Bound(o => o.Company);
       ...
   }))

如果有效,那么您应该尝试使用其他名称创建另一个Action方法。在我的项目中,我这样做:

[GridAction]
public ActionResult GetYeagerTechGridData()
{
    IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
    return View(new GridModel<YeagerTechWcfService.Customer>
    {
        Data = customerList
    });
}

以这种方式使用你的网格:

@(Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>()
   .Name("Customers")
   .Columns(columns =>
   {
       columns.Bound(o => o.Email);
       columns.Bound(o => o.Company);
       ...
   })
   .DataBinding(dataBinding => dataBinding.Ajax().Select("GetYeagerTechGridData", "Customer")))
相关问题