【问题标题】:Kendo Grid changing depending on DropDownList剑道网格根据 DropDownList 变化
【发布时间】:2019-03-11 16:20:19
【问题描述】:

在我开始之前,我只想说我在发布之前查看了其他答案,但没有一个特别帮助我。

我需要在 ASP.NET MVC 中创建一个 Kendo UI 网格,该网格会根据用户从 DropDownList 中选择的内容而变化。我最终将使用数据库中的数据,但目前我正在尝试使用随机硬编码数据进行学习。

我在网上找到了一个教程,该教程向我展示了如何使用示例数据库中的数据进行操作,但由于无法解释的原因,我无法进行设置。因此,我正在尝试调整该教程中的代码以与我的控制器和模型一起使用。这可能设置完全错误,因为我对 ASP.NET MVC 比较陌生。

所以here's 我正在尝试遵循的教程。

这是我的控制器:

       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);
            }

        }
    } 

然后是我的 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; }
    }

我已经尝试在教程中更改视图中的值,但它不起作用,因为我相信它不像更改一些文本那么简单。

我很困惑如何做到这一点,不知道从哪里开始。我知道这是一篇包含大量代码的冗长文章,但我非常感谢一些帮助。

我在调整教程代码时哪里出错了?我必须进行哪些更改才能使其与硬编码数据一起使用?

【问题讨论】:

  • 你说得对,它并不像仅仅改变一些文本那么简单。事实上,我想说剑道网格是最烦人的,因为它们很少给出特定的错误。如果您可以向我们展示您的网格代码以及您想要的效果,我可以为您提供帮助
  • @JamesS 想要的效果是网格将根据在 DropDownList 中选择的内容更改显示的数据。例如:如果有人从 DDL 中选择表 1,则表 1 中的数据将显示在网格中,表 2 反之亦然。我拥有的代码与教程中的代码完全相同,但值已更改为我的桌子的名字等等。
  • 我要做的是将剑道网格粘贴在局部视图中。然后使用 javascript 和 ajax 调用来获取下拉列表的值并将其发送到仅加载网格的控制器方法。将.read 操作添加到您的网格中,该操作只需获取您将传递到此新部分视图中的下拉列表的 ID。从这里您将能够调用 SQL 数据库并使用此数据填充网格。
  • @JamesS 但至少现在我需要从控制器中创建的列表中获取数据。我目前没有可以使用的数据库,或者教程中的代码可以工作。

标签: c# asp.net asp.net-mvc kendo-ui kendo-grid


【解决方案1】:

这并不难。您需要做的是为您想要的每个 Action 更改 DataSource 的 url。因此,根据用户在 DDL 中选择的选项,您可以更改 DataSource。检查this demo

您需要从上面的演示中更改的是,您的网格的 DataSource 将调用 url 而不是硬编码的 json,对吗?在该网址中,您可以更改所需的操作:

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

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

它将读取新的 url 并将数据提取到网格中并更新它。

更新

传输 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
    }
});

【讨论】:

  • 能否请您详细说明如何更改?我对在运输部件内放什么感到有点困惑。
【解决方案2】:

1) 从此视图中移除网格并创建一个新的局部视图,然后将网格放在其中。

现在这可以是两种方式之一。通过下拉列表的 onclick 或 onchange。你的选择

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) 获取下拉列表的值并将其传递给调用此新局部视图的控制器方法,将模型中的 ID 发送给它

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

    return PartialView("_theGridPartial", model);
}

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) 这是读取的新控制器

    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);  
        }  

    } 

这应该允许您根据下拉列表更改表格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多