简体   繁体   English

MVC 4 - Kendo Grid数据绑定

[英]MVC 4 - Kendo Grid Data Binding

Here is everything from my project: 以下是我项目的所有内容:

ControllerA ControllerA

public ActionResult Index()
{
    return View();
}

public ActionResult User_Read()
{
    ViewModels.ViewModelA objView = new ViewModels.ViewModelA();

        objView.MyList = new List<ViewModels.SomeClass>();
        objView.MyList.Add(new ViewModels.SomeClass() { FirstName = "FN1", Supervisor = "SV1" });
        objView.MyList.Add(new ViewModels.SomeClass() { FirstName = "FN2", Supervisor = "SV2" });

        return Json(objView, JsonRequestBehavior.AllowGet);
}

ViewModelA ViewModelA

public class ViewModelA
{
    public List<SomeClass> MyList { get; set; }
}

public class SomeClass
{
    public string FirstName { get; set; }
    public string Supervisor { get; set; }
}

Index.cshtml Index.cshtml

@using Kendo.Mvc.UI

@(Html.Kendo().Grid<Solution1.ViewModels.ViewModelA>()
    .Name("grid")
    .Columns(columns =>
        {
            columns.Bound(c => c.FirstName);
            columns.Bound(c => c.Supervisor);
        })
        .HtmlAttributes(new { style = "height: 380px" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("User_Read", "ControllerA"))
        )
)

Details: When I run this the page loads fine with no errors, however, none of the data is populated on the page. 详细信息:当我运行此页面时,页面加载正常且没有错误,但是,页面上没有填充任何数据。 So, the grid is there but it only shows the column names of FirstName and Supervisor. 因此,网格存在,但它只显示FirstName和Supervisor的列名。 I know you're not supposed to hardcode data, but I'm just trying to show something on the grid for now and I want the data to come from the Controller like it is. 我知道你不应该对数据进行硬编码,但我现在只想在网格上显示一些东西,我希望数据来自Controller。

Question: What do I need to change in order to show data on the grid? 问题:为了在网格上显示数据,我需要更改什么? Also, from the data in the Controller, how can I add more than one row to the grid? 此外,根据Controller中的数据,如何向网格添加多行?

Update: The question I initially asked has partially been solved. 更新:我最初提出的问题已部分解决。 Still need assistance to display the data to the grid. 仍需要帮助才能将数据显示到网格中。

There are two things that you need to do to get your code to work. 要使代码工作,您需要做两件事。

  1. You need to return a list (or in fact anything that supports the IEnumerable interface). 您需要返回一个列表(或实际上支持IEnumerable接口的任何内容)。

  2. You need to format the return data by calling ToDataSourceResult on it. 您需要通过调用ToDataSourceResult来格式化返回数据。

Hence 于是

    public ActionResult User_Read([DataSourceRequest]DataSourceRequest request)
    {
        var model = new List<ViewModelA>()
        {
            new ViewModelA()
            {
                FirstName = "Name",
                Supervisor = "Mgr",
            },
            new ViewModelA()
            {
                FirstName = "FirstName",
                Supervisor = "Supervisor",
            },
        };

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

Everything else is exactly as you had it in your original example before you edited it. 在编辑之前,其他所有内容都与原始示例中的完全相同。

Look at the Kendo demo solution and study the examples. 查看Kendo演示解决方案并研究示例。 It is not the best layed out demo and sometimes you need to go digging in the source code to find out exactly what is going on rather than just relying on what the demo seems to imply you need. 它不是最好的布局演示,有时您需要深入挖掘源代码以确切了解发生了什么,而不仅仅是依赖于演示似乎意味着您需要的内容。 For example in the Populating the Grid via Ajax demo the actual action method that gets called by the Ajax and returns the data is not shown in the demo! 例如,在通过Ajax演示填充网格演示中,由Ajax调用并返回数据的实际操作方法未在演示中显示!

Your need to bind the grid to a list in order to show multiple rows. 您需要将网格绑定到列表以显示多行。

For example: 例如:

public class ViewModelA
{
    public List<SomeClass> MyList { get; set; }
}
public class SomeClass
{
    public string FirstName { get; set; }
    public string Supervisor { get; set; }
}

and then assign values to the list to display them in your grid. 然后将值分配给列表以在网格中显示它们。

ViewModelA objView = new ViewModelA()  ;
objView.MyList = new List<SomeClass>();
objView.MyList.Add(new SomeClass() { FirstName = "Test", Supervisor = "SomeValue" });
objView.MyList.Add(new SomeClass() { FirstName = "AnotherTest", Supervisor = "SomeMore" });

Pass your ViewModelA.MyList to grid to show all elements 将ViewModelA.MyList传递给网格以显示所有元素

@(Html.Kendo().Grid<Solution1.ViewModels.ViewModelA.MyList>()
    .Name("grid")
    .Columns(columns =>
        {
            columns.Bound(c => c.FirstName);
            columns.Bound(c => c.Supervisor);
        })
        .HtmlAttributes(new { style = "height: 380px" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("User_Read", "ControllerA"))
        )
)

here are few changes you have to make 这里有一些你必须做的改变

Your data is available in MyList but you are creating the grid for ViewModelA , So you have to return the MyList from the controller and construct the grid with that list. 您的数据在MyList中可用,但您正在为ViewModelA创建网格,因此您必须从控制器返回MyList并使用该列表构建网格。

In Controller 在控制器中

    public ActionResult User_Read()
    {
        ViewModels.ViewModelA objView = new ViewModels.ViewModelA();

            oViewModelA objView = new ViewModelA()  ;
            objView.MyList = new List<SomeClass>();
            objView.MyList.Add(new SomeClass() { FirstName = "Test", Supervisor =                   "SomeValue" });
            objView.MyList.Add(new SomeClass() { FirstName = "AnotherTest", Supervisor = "SomeMore" });

            return Json(objView.MyList, JsonRequestBehavior.AllowGet);
    }

In the Index.cshtml 在Index.cshtml中

@using Kendo.Mvc.UI

@(Html.Kendo().Grid<SomeClass>()
    .Name("grid")
    .Columns(columns =>
        {
            columns.Bound(c => c.FirstName);
            columns.Bound(c => c.Supervisor);
        })
        .HtmlAttributes(new { style = "height: 380px" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("User_Read", "ControllerA"))
        )
)

Hope this helps. 希望这可以帮助。

What you have should work, from what I see, just change your return statement to this: 你所拥有的应该是有用的,从我看到的,只需将你的return语句更改为:

return Json(objView.MyList.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

You already have the .Read(read => read.Action("User_Read", "ControllerA")) , so I don't know where others are saying that's something new to add or a revelation. 你已经有了.Read(read => read.Action("User_Read", "ControllerA")) ,所以我不知道其他人在说什么是新增内容或启示。 You can make the list be a DataSourceResult directly and just return that. 您可以直接将列表设为DataSourceResult,然后返回该列表。

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

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