In My MVC Web Application I Call Ajax and In Success I have Json Object ,The Json Object Contains list of items ,the length of list is 61111 item but when Trying to append these items to the list one by one using loop it takes long time and then the web page tells me that the page unresponsive kill or continue,
the code as follow
step1:
@Html.ListBox("_TAG_INFO", new MultiSelectList(ViewBag.TAG_INFO_ID, "ID", "ITEM_ID", Model._TAG_INFO.Select(x => x.ID)), new { Multiple = "multiple", @id = "TagsListbox" })
--
Step2:
$.ajax({
cache: false,
type: "POST",
url: "@(Url.Action("GetTags", "TagPath"))",
data: { "Service": service },
dataType: 'json',
success: function (data) {
$("#TagsListbox").empty();
for (var i = 0; i < data.objects.length; i++) {
var selected = "";
if (data.objects[i] != null) {
if (data.objects[i]._IsSelected) {
selected = "selected";
}
var newOption = "<option value='" + data.objects[i].ID + "'" + " " + selected + ">" + data.objects[i].ITEM_ID + "</option>";
$("#TagsListbox").append(newOption);
}
}
},
error: function () {
alert('@Resource._CantRetData');
}
});
-----
step3:
[HttpPost]
public JsonResult GetTags(string service)
{
string strAlert = string.Empty;
List<TAG_INFO> objects = new List<TAG_INFO>();
int CurrentBusinessGroup = GetCurrentUserGroup().BUSINESS_GROUP_ID;
List<TAG_INFO> _TAG_INFO;
try
{
var FilteredTags =new List<TAG_INFO>();
if (service == "0" || service == null)
{
_TAG_INFO = db.TAG_INFO.Where(x => x.STATUS == ActiveStatus).ToList();
}
else
{
_TAG_INFO = db.TAG_INFO.Where(x => x.SERVICE == service && x.STATUS == ActiveStatus).ToList();
}
if (_TAG_INFO.Count > 0)
{
foreach (var item in _TAG_INFO)
{
objects.Add(new TAG_INFO() { ID = item.ID, ITEM_ID = item.ITEM_ID + "-" + item.ITEM_SUB_ID, _IsSelected = false });
}
}
}
catch (Exception ex)
{
strAlert = ex.ToString();
}
JsonResult jsonResult = Json(new { objects = objects, strAlert = strAlert },JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
What I have tried:
I am trying to append More than Millon record to listBox.