级联下拉列表数据源

时间:2021-08-15 07:15:36

My team is working on a web application using MVC4 with .NET 4.5. One task is to create a parent drop down list that will update a child drop down list when a value is selected in the parent - for example a drop down list of states would filter a drop down list of cities when a state is selected.

我的团队正在使用MVC4和.NET 4.5开发Web应用程序。一个任务是创建父下拉列表,该列表将在父项中选择值时更新子下拉列表 - 例如,状态下拉列表将在选择状态时过滤城市下拉列表。

On our other projects we use Ajax calls to a web service or database to populate the values of a child when a parent value is selected. In my example above, the state of Minnesota is selected, and an Ajax call is made to the database to grab all cities in MN. I have read other posts on here and tutorials that follow this concept.

在我们的其他项目中,我们使用Ajax调用Web服务或数据库来在选择父值时填充子项的值。在上面的示例中,选择了明尼苏达州,并且对数据库进行了Ajax调用以获取MN中的所有城市。我已经阅读了此处的其他帖子以及遵循此概念的教程。

A new developer on our team believe the method above is inefficient and it is better to grab all cities on page load, store the cities as a Jason object in a JavaScript variable and then use jquery to loop through the JavaScript variable and build the child list.

我们团队的新开发人员认为上面的方法效率低,最好在页面加载时抓住所有城市,将城市作为Jason对象存储在JavaScript变量中,然后使用jquery循环遍历JavaScript变量并构建子列表。

Can someone give me feedback on which method they have used? I don't believe pulling thousands of records into the browser and storing on the client is very efficient when we may only show 10 of the records. This developer also believes looping through a 10000 row JSON object in JavaScript is faster than making a database call to grab 10 rows.

有人可以给我反馈他们使用的方法吗?我不相信将数千条记录拖入浏览器并且当我们只显示10条记录时,存储在客户端上是非常有效的。此开发人员还认为在JavaScript中循环遍历10000行JSON对象比使数据库调用以获取10行更快。

1 个解决方案

#1


1  

Create your list and you can call everything from LINQ. Check the cache to create from there before you do your other calls to instantiate

创建列表,您可以从LINQ调用所有内容。在进行其他实例化调用之前,请检查缓存以从那里创建

public IList<Example> GetList(string _state)
{

    IList<Example> cities = new List<Example>();

    Cache cache = HttpContext.Current.Cache;

    if (cache[_state] != null)
        cities = (IList<Example>)cache[_state];

    else
    {
        //Do your calls
        cache.Insert(_state, cities);
    }

    return cities;
}

#1


1  

Create your list and you can call everything from LINQ. Check the cache to create from there before you do your other calls to instantiate

创建列表,您可以从LINQ调用所有内容。在进行其他实例化调用之前,请检查缓存以从那里创建

public IList<Example> GetList(string _state)
{

    IList<Example> cities = new List<Example>();

    Cache cache = HttpContext.Current.Cache;

    if (cache[_state] != null)
        cities = (IList<Example>)cache[_state];

    else
    {
        //Do your calls
        cache.Insert(_state, cities);
    }

    return cities;
}