简体   繁体   English

与 ASP.Net MVC 和 Kendo Grid 的数据绑定

[英]Data binding with ASP.Net MVC and Kendo Grid

I'm new to working with ASP.Net MVC and Kendo controls.我是使用 ASP.Net MVC 和 Kendo 控件的新手。

I have looked at this demo page: Kendo Grid Binding to local data我看过这个演示页面: Kendo Grid Binding to local data

and it seems that it shouldn't be hard at all, and yet I don't know what I'm missing.似乎一点也不难,但我不知道我错过了什么。

I get the data from a function that retrieves it from a SQL database in SurveyDataManager.cs.我从 function 获取数据,该数据从 SurveyDataManager.cs 中的 SQL 数据库中检索。

public static List<SurveySummary> GetSurveySummary(int resourceTag)
    {
        List<SurveySummary> surveySummaryList = new List<SurveySummary>();

        try
        {
            using (ESSDataContext ess_context = new ESSDataContext())
            {
                var surveySummaryRec = ess_context.fn_ESS_SurveySummary(resourceTag).ToList();

                foreach (var item in surveySummaryRec)
                {
                    SurveySummary surveySummary = new SurveySummary()
                    {
                        SurveyID = item.SurveyHeaderID,
                        SurveyName = item.SurveyName,
                        SurveyInstructions = item.SurveyInstructions,
                        SurveyDescription = item.SurveyDescription,
                        Mandatory = item.Mandatory,
                        DueDate = item.DueDate,
                        Status = item.Status

                    };
                    surveySummaryList.Add(surveySummary);
                }                    
               
            }
        }
        catch (Exception e)
        {
            log.Error("Getting the survey summary failed for user with resource tag: " + resourceTag);
            Elmah.ErrorSignal.FromCurrentContext().Raise(e);
        }

        return surveySummaryList;

    }

This is called by the Controller (HomeController's Index):这由 Controller(HomeController 的索引)调用:

 public ActionResult Index()
    {
        var surveySummary = SurveyDataManager.GetSurveySummary(User.GetResourceTag());
        return PartialView("~/Areas/Survey/Views/MySurveys.cshtml", surveySummary);         
    }

Here is the code in my MySurveys.cshtml file:这是我的 MySurveys.cshtml 文件中的代码:

@using Kendo.Mvc
@using Entities.Survey


@(Html.Kendo().Grid<SurveySummary>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.SurveyName).Title("Survey Name").Width(100);
                columns.Bound(p => p.SurveyDescription).Title("Description").Width(150);
                columns.Bound(p => p.Originator).Title("Originator").Width(100);
                columns.Bound(p => p.DueDate).Title("Due Date").Width(125);
                columns.Bound(p => p.Status).Title("Status").Width(140);
            })
            .HtmlAttributes(new { style = "height: 550px;" })
            .Scrollable()
            .Groupable()
            .Sortable()
            .Pageable(pageable => pageable
                .Refresh(true)
                .PageSizes(true)
                .ButtonCount(5))
            .DataSource(dataSource => dataSource
                .Ajax()
                //.Read(read => read.Action("Index", "Home"))
                .PageSize(20)
            )
)

I get an empty grid, with the correct column names (titles), but no content.我得到一个空网格,列名(标题)正确,但没有内容。 The SurveySummaryList definitely has at least one item in it, but it is not shown in the grid. SurveySummaryList 肯定至少有一项,但它没有显示在网格中。

What am I doing wrong?我究竟做错了什么?

Did you try @(Html.Kendo().Grid<SurveySummary>(Model) instead of @(Html.Kendo().Grid<SurveySummary>() ?您是否尝试@(Html.Kendo().Grid<SurveySummary>(Model)而不是@(Html.Kendo().Grid<SurveySummary>()

So I realised I was sending a list back, so I needed to change the cshtml file to:所以我意识到我正在发回一个列表,所以我需要将 cshtml 文件更改为:

@model IEnumerable<Symplexity.ESS.Entities.Survey.SurveySummary>


@(Html.Kendo().Grid(Model)
...

Instead of what I had done previously as can be seen in the original post.而不是我以前在原始帖子中看到的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM