Problem in JsonResult
I am using the jsonresult for listing all the cities under a country. I retrieve the data from database and loa开发者_如何学JAVAd it to a dropdown list using jquery. The problem is if the cities goes beyond 3000 then the jsonresult is not working.
<script type="text/javascript">
$(document).ready(function () {
//Hook onto the MakeID list's onchange event
$("#Country").change(function () {
//build the request url
$("#HomeTown").empty();
var url = '<%= Url.Content("~/") %>' + "Location/GetCitiesByCountry/" + $("#Country").val();
$.getJSON(url, function (data) {
$.each(data, function (index, optionData) {
$("#HomeTown").append("<option value='" + optionData.geonameid + "'>" + optionData.asciiname + "</option>");
});
$("#HomeTown").option[0].selected = true;
});
}).change();
});
</script>
Too many items in the dropdown.
Either limit the items, or consider using another control - such as an AutoComplete control.
I agree with @Ken. What you might be able to do is a progressive load of the data.
So you could load in the first say 500 and while those are displaying you could load the next 500 using jQuery.
Or, and you need to research this, you can probably get a jQuery drop down which allows you to do a post back when you hit the bottom of the dropdown.
But I think the best option is to try and limit the amount of data in the dropdown.
3000 is an awfully large number for a dropdown list. You should consider some sort of ajax interaction with your server to page results as needed. Something like the jQuery DataTables plugin (or any number of similar options, as discussed here) should give you a good start. Some of these can be simplified so that they're little more than a dropdown list, visually -- but with server-side paging.
http://forums.asp.net/p/1090853/1634922.aspx This link has a discussion regarding maximum length exceeded errors from json serialization. Apparently you can increase the max size in web.config.
Apart from being slow to load or possibilities of crashing, a 3000 item list in a web page is a poor design from the user experience point of view. Imagine the size of the page when the drop-down list is open. I wouldn't possibly come back to a site that gives me such a big list to choose from.
Instead of having whole 3000 cities in the list, think of a way to page them. Classify the cities based on their location(country/state/province) or name (alphabetic order) or whatever criteria that makes sense for your application.
精彩评论