Click here to Skip to main content
14,921,143 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
C#
 public ActionResult GetAllCity()
        {
            CountryBL objbl = new CountryBL();
            var model = new MyCustomerModel();
            var itmlist = objbl.FetchCity();
            model.CityList = itmlist.Select(x =>
            {
                return new MyCustomerModel()
                {
                    MCCity = x.Name,
                    MCZip = x.ZipCode,
                    MCStateId = x.StateId,
                    StateAbbreviation = x.StateAbbreviation,
                    MCShippingState = x.StateName
                };
            }).ToList();

            var jsonResult = Json(model.CityList, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }


<script>
var $ac = jQuery.noConflict();
    var Zip = [];
    var City = [];
    var State = [];
    $ac(document).ready(function () {
        debugger;
        $ac.ajax({
            url: '@Url.Action("GetAllCity", "Account")',
            type: "GET",
            success: function (data) {
                debugger;
                if (data != null) {
                    //for (var k in data) {
                    //    Zip.push(data[k].MCZip);
                    //    City.push(data[k].MCCity);
                    //    //State.push(data[k].MCShippingState);
                    //}
                    Zip.push(data);
                    //Zip.push(data.MCCity);

                    autocompletezipcodeBilling(Zip);
                    autocompletezipcodeShipping(Zip);
                    autocompleteCityBilling(City);
                    autocompletezipCityShipping(City);
                }
            }
        });
        });
</script>


I need this because when i put large amount of data into loop, it takes top much time to push data inside array.So, if anyone have solution please help.
Thanks.
Posted
Updated 19-Apr-17 21:16pm
v4
Comments
George Jonsson 9-Nov-15 1:02am
   
Is AngularJS an alternative?
Joy Acharya,90 14-Jun-16 0:14am
   
When you are fetching city in item List , here just select Name,Zipcode,StateId,StateAbbreviation ,StateName. Then you don't need any loop. Just return the itemList as a Json Result.

You can try JQuery.each() function
JavaScript
var $ac = jQuery.noConflict();   
    $ac(document).ready(function () {
        debugger;
        $ac.ajax({
            url: '@Url.Action("GetAllCity", "Account")',
            type: "GET",
            success: function (data) {
                debugger;
                if (data != null) {
                 $ac.each(data, function(index, item) {
                    autocompletezipcodeBilling(item.MCZip);
                    autocompletezipcodeShipping(item.MCZip);
                    autocompleteCityBilling(item.MCCity);
                    autocompletezipCityShipping(item.MCCity);
                   });                 
                }
            }
        });
        });
   
C#
public ActionResult GetAllCity()
        {
            CountryBL objbl = new CountryBL();
            var model = new MyCustomerModel();
            var itmlist = objbl.FetchCity();
            model.CityList = itmlist.Select(x =>
            {
                return new MyCustomerModel()
                {
                    MCCity = x.Name,
                    MCZip = x.ZipCode,
                    MCStateId = x.StateId,
                    StateAbbreviation = x.StateAbbreviation,
                    MCShippingState = x.StateName
                };
            }).ToList();
            var citylist = model.CityList.Select(x => x.MCCity).ToArray();
            var Ziplist = model.CityList.Select(x => x.MCZip).ToArray();
            var jsonResult = Json(new { citylist = citylist, Ziplist=Ziplist }, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }
<script>
var $ac = jQuery.noConflict();
    var Zip = [];
    var City = [];
    var State = [];
    $ac(document).ready(function () {
        debugger;
        $ac.ajax({
            url: '@Url.Action("GetAllCity", "Account")',
            type: "GET",
            success: function (data) {
                debugger;
                if (data != null) {
                   
                    City=data.citylist;
                    Zip=data.Ziplist;
                    autocompletezipcodeBilling(Zip);
                    autocompletezipcodeShipping(Zip);
                    autocompleteCityBilling(City);
                    autocompletezipCityShipping(City);
                }
            }
        });
});
</script>
   
Comments
Patrice T 9-Nov-15 2:04am
   
You did not removed loops, you change the way you populate the arrays.
manosabari 7-May-20 8:32am
   
Nice answer
I fear you ask the question the wrong way.
To assign large amount of data to array, it always involve a loop or many, hidden or explicit, not matter what, there is a loop.

I think the loop is not the root of the problem. You should rather explain us what you do with more details, so we can guess how to handle the problem.
The size of the data is an interesting detail.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900