简体   繁体   English

剑道网格根据 DropDownList 变化

[英]Kendo Grid changing depending on DropDownList

Before I start I'll just say that I've looked at other answers before posting and none specifically help me.在我开始之前,我只想说我在发布之前查看了其他答案,但没有一个专门帮助我。

I need to create a Kendo UI grid in ASP.NET MVC that changes depending on what the users selects from a DropDownList.我需要在 ASP.NET MVC 中创建一个 Kendo UI 网格,它会根据用户从 DropDownList 中选择的内容而改变。 I will eventually be using data from a database, but currently I'm trying to learn with random hard-coded data.我最终将使用来自数据库的数据,但目前我正在尝试使用随机硬编码数据进行学习。

I found a tutorial online that shows me how to do it with data from a sample database, but I can't set that up for reasons I cant explain.我在网上找到了一个教程,该教程向我展示了如何使用示例数据库中的数据进行操作,但由于无法解释的原因,我无法进行设置。 So I'm trying to adapt the code from that tutorial to work with my controllers and models.因此,我正在尝试调整该教程中的代码以与我的控制器和模型一起使用。 This might be set up completely wrong as I'm relatively new to ASP.NET MVC.这可能设置完全错误,因为我对 ASP.NET MVC 比较陌生。

So here's the tutorial I'm trying to follow.所以这是我试图遵循的教程。

This is my controller:这是我的控制器:

       public class LookupValueController : Controller
        {
            private List<LookupModel> tables = new 
List<LookupModel>()
                { new LookupModel() { TableName = "Table1", 
Description = "Table 1" },
                  new LookupModel() { TableName = "Table2", 
Description = "Table 2" } };

        private List<LookupValueModel> values = new List<LookupValueModel>()
            { new LookupValueModel() { TableName = "Table1", Description = "Value 1", LookupCode = "1" },
              new LookupValueModel() { TableName = "Table2", Description = "Value 2", LookupCode = "2"} };


        // GET: LookupValue
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllTableA()
        {
            try
            {

                var table = tables;

                return Json(table, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }

        }

        public ActionResult GetAllTableB()
        {
            try
            {

                var value = values;

                return Json(value, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }

        }
    } 

Then my 2 models:然后我的 2 个模型:

    public class LookupValueModel
    {
        public string TableName { get; set; }
        public string LookupCode { get; set; }
        public string Description { get; set; }
    } 

    public class LookupModel
    {
        public string TableName { get; set; }
        public string Description { get; set; }
    }

I've tried just changing the values in the view in the tutorial but it doesn't work, as I believe it isn't as simple as just changing some text.我已经尝试在教程中更改视图中的值,但它不起作用,因为我相信它不像更改一些文本那么简单。

I'm pretty stuck for how to do this and don't know where to go from here.我对如何做到这一点很困惑,不知道从哪里开始。 I know this is a very long winded post with lots of code, but I would really appreciate some help.我知道这是一篇很长的文章,有很多代码,但我真的很感激一些帮助。

Where am I going wrong adapting the tutorial code?调整教程代码我哪里出错了? What do I have to change to get it to work with hard-coded data?我必须改变什么才能让它与硬编码数据一起工作?

That's not that hard.那没那么难。 What you need to do is to change the DataSource's url for each Action you want.您需要做的是为您想要的每个操作更改数据源的 url。 So, depending on what options user selects in the DDL, you change the DataSource.因此,根据用户在 DDL 中选择的选项,您可以更改数据源。 Check this demo .检查这个演示

What you need to change in from the above demo is that your grid's DataSource will call an url instead of a hard-coded json, right?您需要从上面的演示中更改的是,您的网格的 DataSource 将调用 url 而不是硬编码的 json,对吗? In that url, you change the desired action:在该网址中,您更改所需的操作:

let changeTableData = function changeTableData(option) {
    let dataSource = new kendo.data.DataSource({
          transport: {
              url: "MyApp/" + option
          }
        });

    $("#grid").data("kendoGrid").setDataSource(dataSource);
};

It will read the new url and fetch the data into the grid and updated it.它将读取新的 url 并将数据提取到网格中并更新它。

UPDATE更新

The transport url ir the url path to your action, eg传输 url 是您操作的 url 路径,例如

let url;
if (option == "A") {
    url = "@Url.Action("TableA")";
} 
else if (option == "B") {
    url = "@Url.Action("TableB")";
}

let dataSource = new kendo.data.DataSource({
    transport: {
        url: url
    }
});

1) Remove the grid from this view and create a new partialview and just have the grid located in that. 1)从此视图中删除网格并创建一个新的局部视图,并将网格放在其中。

Now this can be one of two ways.现在这可以是两种方式之一。 Either an onclick via the drop down list or an onchange.通过下拉列表的 onclick 或 onchange。 Your choice你的选择

function Getdropdown()
{
    var id = $("#//dropdownID").val();  //Get the dropdown value

    var json = '{dropdownId: ' + id + '}';
    $.ajax({
        url:'@Url.Action("ViewGrid", "//Controller")',
        type:'POST',
        data:json,
        contentType:'Application/json',
        success:function(result){
            $("//The Id of of the div you want the partial to be displayed in in the cshtml").html(result);
        }
    });

}

2) Get the value of the dropdown and pass it to a controller method that calls this new partial view, sending it the ID in a model 2) 获取下拉列表的值并将其传递给调用此新局部视图的控制器方法,并将模型中的 ID 发送给它

public ActionResult ViewGrid(int dropdownId)
{
    AModel model = new AModel
    {
        DropDownID = dropdownId
    };

    return PartialView("_theGridPartial", model);
}

3) Change your grid to look like this: 3)改变你的网格看起来像这样:

 @(Html.Kendo().Grid<KendoMvcApp.Models.EmployeeA>()  
    .Name("EmpGrid")  
    .Selectable()  
    .Columns(columns =>  
    {  

        columns.Bound(c => c.FirstName);  
        columns.Bound(c => c.LastName);  

    })  
    .DataSource(dataSource => dataSource  
        .Ajax()  
        .Read(read => read.Action("GetAllEmployee", "GridDataSource", new {id = Model.DropDownID}))  
    )  
) 

4) This is the new Controller read 4)这是新的控制器读取

    public ActionResult GetAllEmployee([DataSourceRequest]DataSourceRequest request, int id)  
    {  
        try  
        {  
            //Have a call that gets the table data based on the id you are passing into here. This id will be the table id you have got from your dropdown list 
        }  
        catch (Exception ex)  
        {  
            return Json(ex.Message);  
        }  

    } 

This should allow you to change the table based on the dropdown.这应该允许您根据下拉列表更改表格。

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

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