Asp.net mvc 加速页面加载和信息处理

Asp.net mvc 加速页面加载和信息处理,asp.net-mvc,linq-to-sql,Asp.net Mvc,Linq To Sql,在我的web应用程序中,我正在加载一个页面,它可以在一个表中加载多达8000行或更多行,每行都有自己的dropdownlist。第一个过程被证明是非常低效的,但我被要求这样做。加载行的代码如下所示: <tbody> <% var i = 0; foreach (var row in Model) { var comp = "ok"; if (

在我的web应用程序中,我正在加载一个页面,它可以在一个表中加载多达8000行或更多行,每行都有自己的dropdownlist。第一个过程被证明是非常低效的,但我被要求这样做。加载行的代码如下所示:

 <tbody>
        <%  var i = 0;



            foreach (var row in Model)
            {
                var comp = "ok";
                if (row.LidExpected != (string.IsNullOrEmpty(row.LidObtained) ? null : row.LidObtained) || row.QuantityExpected != row.QuantityObtained)
                {
                    comp = "ko";
                }
                %>
        <tr class="child_row <%= comp %>">
            <input type="hidden" name="Goods.index" value="<%= i %>" />
            <td class="field <%= InventoryGoods.Metadata.Gid.CssClass %>">
              <%--  <%= Html.Encode(row.Gid) %>--%>
              <%--http://tecnicos.urbanos.com/Goods/Details/... --%>
               <%= Html.ActionLink(row.Gid, "Details", "Goods", new {id = row.Gid}, null) %>
                <%= Html.Hidden(String.Format("Record[{0}].Gid", i), row.Gid) %>
            </td>
            <td class="field <%= InventoryGoods.Metadata.LidExpected.CssClass %>">
                <%= Html.Encode(row.LidExpected) %>
                <%= Html.Hidden(string.Format("Record[{0}].LidExpected", i), row.LidExpected)%>
            </td>
            <td class="fieldRight <%= InventoryGoods.Metadata.QuantityExpected.CssClass %>">
                <%= Html.Encode(row.QuantityExpected) %>
                <%= Html.Hidden(string.Format("Record[{0}].QuantityExpected", i), row.QuantityExpected)%>
            </td>
            <td class="field <%= InventoryGoods.Metadata.LidObtained.CssClass %>">
                <%= Html.Encode(row.LidObtained) %>
                <%= Html.Hidden(string.Format("Record[{0}].LidObtained", i), row.LidObtained)%>
            </td>
            <td class="fieldRight <%= InventoryGoods.Metadata.QuantityObtained.CssClass %>">
                <%= Html.Encode(row.QuantityObtained) %>
                <%= Html.Hidden(string.Format("Record[{0}].QuantityObtained", i), row.QuantityObtained)%>
            </td>
             <%if (int.Parse(state.ToString()) == (int)InventoryStateEnum.Closed || int.Parse(state.ToString()) == (int)InventoryStateEnum.Verified)
               { %>
            <td class="field <%= InventoryGoods.Metadata.Action.CssClass %>">
                <%                        
                    switch (row.Action)
                    {
                        case (int)InventoryGoodsActionEnum.AdjustQuantity: %>
                             <%=    Html.DropDownList(String.Format("Record[{0}].Action", i), new[]
                    {
                        new SelectListItem { Text= "Ajustar Quantidade", Value= ((int)InventoryGoodsActionEnum.AdjustQuantity).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantity == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ignorar", Value= ((int)InventoryGoodsActionEnum.Ignore).ToString(), Selected= (int)InventoryGoodsActionEnum.Ignore == row.Action ? true : false},
                    })%>
                      <%      break;
                        case (int)InventoryGoodsActionEnum.AdjustLocation: %>
                             <%=    Html.DropDownList(String.Format("Record[{0}].Action", i), new[]
                    {

                        new SelectListItem { Text= "Relocalizar", Value= ((int)InventoryGoodsActionEnum.AdjustLocation).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustLocation == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ignorar", Value= ((int)InventoryGoodsActionEnum.Ignore).ToString(), Selected= (int)InventoryGoodsActionEnum.Ignore == row.Action ? true : false},
                    })%>
                          <%  break;
                        case (int)InventoryGoodsActionEnum.AdjustStockType:  %>
                             <%=    Html.DropDownList(String.Format("Record[{0}].Action", i), new[]
                    {                        
                        new SelectListItem { Text= "Ajustar Tipo de Stock", Value = ((int)InventoryGoodsActionEnum.AdjustStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustStockType == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ignorar", Value= ((int)InventoryGoodsActionEnum.Ignore).ToString(), Selected= (int)InventoryGoodsActionEnum.Ignore == row.Action ? true : false},
                    })%>
                         <%   break;
                        case (int)InventoryGoodsActionEnum.AdjustQuantityLocation:  %>
                             <%=    Html.DropDownList(String.Format("Record[{0}].Action", i), new[]
                    {
                        new SelectListItem { Text= "Ajustar Quantidade", Value= ((int)InventoryGoodsActionEnum.AdjustQuantity).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantity == row.Action ? true : false},
                        new SelectListItem { Text= "Relocalizar", Value= ((int)InventoryGoodsActionEnum.AdjustLocation).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustLocation == row.Action ? true : false},
                        new SelectListItem { Text= "Ajustar Quantidade e Relocalizar", Value= ((int)InventoryGoodsActionEnum.AdjustQuantityLocation).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantityLocation == row.Action ? true : false},                                                
                        new SelectListItem { Text= "Ignorar", Value= ((int)InventoryGoodsActionEnum.Ignore).ToString(), Selected= (int)InventoryGoodsActionEnum.Ignore == row.Action ? true : false},
                    })%>
                          <%  break;
                        case (int)InventoryGoodsActionEnum.AdjustQuantityStockType: %>
                             <%=    Html.DropDownList(String.Format("Record[{0}].Action", i), new[]
                    {
                        new SelectListItem { Text= "Ajustar Quantidade", Value= ((int)InventoryGoodsActionEnum.AdjustQuantity).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantity == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ajustar Tipo de Stock", Value = ((int)InventoryGoodsActionEnum.AdjustStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustStockType == row.Action ? true : false},
                        new SelectListItem { Text= "Ajustar Quantidade e Tipo de Stock", Value = ((int)InventoryGoodsActionEnum.AdjustQuantityStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantityStockType == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ignorar", Value= ((int)InventoryGoodsActionEnum.Ignore).ToString(), Selected= (int)InventoryGoodsActionEnum.Ignore == row.Action ? true : false},
                    })%>
                         <%   break;
                        case (int)InventoryGoodsActionEnum.AdjustLocationStockType: %>
                             <%=    Html.DropDownList(String.Format("Record[{0}].Action", i), new[]
                    {

                        new SelectListItem { Text= "Relocalizar", Value= ((int)InventoryGoodsActionEnum.AdjustLocation).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustLocation == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ajustar Tipo de Stock", Value = ((int)InventoryGoodsActionEnum.AdjustStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustStockType == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ajustar Tipo de Stock e Relocalizar", Value = ((int)InventoryGoodsActionEnum.AdjustLocationStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustLocationStockType == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ignorar", Value= ((int)InventoryGoodsActionEnum.Ignore).ToString(), Selected= (int)InventoryGoodsActionEnum.Ignore == row.Action ? true : false},
                    })%>
                         <%   break;
                        case (int)InventoryGoodsActionEnum.AdjustQuantityLocationStockType: %>
                             <%=    Html.DropDownList(String.Format("Record[{0}].Action", i), new[]
                    {
                        new SelectListItem { Text= "Ajustar Quantidade", Value= ((int)InventoryGoodsActionEnum.AdjustQuantity).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantity == row.Action ? true : false},
                        new SelectListItem { Text= "Relocalizar", Value= ((int)InventoryGoodsActionEnum.AdjustLocation).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustLocation == row.Action ? true : false},
                        new SelectListItem { Text= "Ajustar Quantidade e Relocalizar", Value= ((int)InventoryGoodsActionEnum.AdjustQuantityLocation).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantityLocation == row.Action ? true : false},                        
                        new SelectListItem { Text= "Ajustar Tipo de Stock", Value = ((int)InventoryGoodsActionEnum.AdjustStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustStockType == row.Action ? true : false},
                        new SelectListItem { Text= "Ajustar Quantidade e Tipo de Stock", Value = ((int)InventoryGoodsActionEnum.AdjustQuantityStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantityStockType == row.Action ? true : false},
                        new SelectListItem { Text= "Ajustar Tipo de Stock e Relocalizar", Value = ((int)InventoryGoodsActionEnum.AdjustLocationStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustLocationStockType == row.Action ? true : false},
                        new SelectListItem { Text= "Ajustar Quantidade, Tipo de Stock e Relocalizar", Value = ((int)InventoryGoodsActionEnum.AdjustQuantityLocationStockType).ToString(), Selected= (int)InventoryGoodsActionEnum.AdjustQuantityLocationStockType == row.Action ? true : false},
                        new SelectListItem { Text= "Ignorar", Value= ((int)InventoryGoodsActionEnum.Ignore).ToString(), Selected= (int)InventoryGoodsActionEnum.Ignore == row.Action ? true : false},
                    })%>
                        <%    break;
                    }


                %>
                <%= Html.Hidden(string.Format("db_Action[{0}]", i), row.Action)%>
            </td>
            <td>
                <a title="Apagar Evento" id="delete_Event" class="ui-widget ui-state-default ui-icon ui-icon-trash">
                </a>
            </td>
            <td class="field <%= InventoryGoods.Metadata.ActionOn.CssClass %>" id="dateCell">
                <span id="ActionOn_<%= i %>">
                    <%= Html.Encode(row.ActionOn.HasValue ? Html.FormatDateTime(row.ActionOn.Value) : Html.Encode(""))%></span>
                <%= Html.Hidden(String.Format("Record[{0}].ActionOn", i), row.ActionOn.HasValue ? row.ActionOn.Value : new DateTime())%>
            </td>
            <% } %>
            <%= Html.Hidden("lineVal", i) %>
            <%  i++;
            } %>
        </tr>
    </tbody>
所以这是所有问题的根源。 下一个问题是何时需要将这些数据发送回服务器,这当然也会占用很多时间。我不知道如何让它更快,因为现在需要8到9分钟才能将9000行发送回服务器

当我使用这些行进行批量更新时,问题仍然存在,如下所示:

public void UpdateInventoryGoods(List<InventoryGoods> list, int id)
        {
            //int index = 0;

            var query = from inventoryGoods in context.InventoryGoods
                        where inventoryGoods.ParentId == id
                        select inventoryGoods;

            List<InventoryGoods> goodsList = query.ToList();
            var memmoryInventoryGoodsEvent = context.InventoryGoodsEvents.ToList();//obter apenas o do Id

            using (var scope = new TransactionScope())
            {
                var events = from g in context.GoodsEvent
                             select g;
                List<GoodsEvent> goodsEventList = events.ToList();

                foreach (InventoryGoods i in list)
                {
                    foreach (InventoryGoods e in goodsList)
                    {
                        //if (index == 30)
                        //{
                        //    index = 0;
                        //    context.SubmitChanges();
                        //}
                        var eventId = getEventId(e.Id, memmoryInventoryGoodsEvent);
                        if (e.Gid == i.Gid && !eventId.HasValue && !e.ActionOn.HasValue)
                        {
                            e.Action = i.Action;

                        }
                        else if ((e.Gid == i.Gid && eventId.HasValue) && (e.Action != i.Action || i.ActionOn == DateTime.MinValue))
                        {
                            e.Action = i.Action;
                            e.ActionOn = null;



                            var inventoryGoodsEventsList = memmoryInventoryGoodsEvent.Where(x => x.InventoryGood == e.Id); 





                            foreach (InventoryGoodsEvents goodsEvent in inventoryGoodsEventsList)
                            {
                                context.InventoryGoodsEvents.DeleteOnSubmit(goodsEvent);

                                foreach (GoodsEvent ge in goodsEventList)
                                {
                                    if (ge.Id == goodsEvent.EventId)
                                    {
                                        ge.IsDeleted = true;
                                        ge.DeletedOn = DateTime.Now;
                                        ge.DeletedBy = System.Web.HttpContext.Current.User.Identity.Name;
                                    }
                                }
                            }




                        }
                        //++index;
                    }
                }
                context.SubmitChanges();
                scope.Complete();
            }

        }

   public int? getEventId(int InventoryGood,List<InventoryGoodsEvents> memmoryList)
        {
            //var firstinventoryGoodsEvents = context.InventoryGoodsEvents.Where(i => i.InventoryGood == InventoryGood).FirstOrDefault();
            var firstinventoryGoodsEvents = memmoryList.Where(i => i.InventoryGood == InventoryGood).FirstOrDefault();

            if (firstinventoryGoodsEvents != null && firstinventoryGoodsEvents.InventoryGood > 0)
            {
                return firstinventoryGoodsEvents.InventoryGood;

            }
            else
            {
                return null;
            }
        }
上面double foreach迭代中的i和e变量表示我从视图中获得的行和数据库中的行。我对它们进行对比以找出任何差异,如果存在差异,则更新它们。 但所有这些过程都太慢了,我真的需要尽快改进。特别是双foreach迭代,我找不到更好的解决方案

你们能帮我吗

编辑:我通过使用字典和转义我在以前的代码中所做的双foreach迭代,至少修复了update方法,如下所示:

public void UpdateInventoryGoods(List<InventoryGoods> list, int id)
    {
        //int index = 0;

        var query = from inventoryGoods in context.InventoryGoods
                    where inventoryGoods.ParentId == id
                    select inventoryGoods;

        Dictionary<string, InventoryGoods> goodsDictionary = query.ToDictionary(p => p.Gid);
        var memmoryInventoryGoodsEvent = (from c in context.InventoryGoodsEvents
                                         join a in context.InventoryGoods on c.InventoryGood equals a.Id
                                         where a.ParentId == id
                                         select c).ToList();//obter apenas o do Id

        using (var scope = new TransactionScope())
        {
            var events = from g in context.GoodsEvent
                         select g;

            Dictionary<int, GoodsEvent> goodsEventDictionary = events.ToDictionary(p => p.Id);

            //List<GoodsEvent> goodsEventList = events.ToList();

            foreach (InventoryGoods i in list)
            {

                var eventId = getEventId(i.Id, memmoryInventoryGoodsEvent);

                var objectToUpdate = goodsDictionary[i.Gid];


                if (!eventId.HasValue && !objectToUpdate.ActionOn.HasValue)
                {
                    objectToUpdate.ActionOn = i.ActionOn;
                    continue;
                }
                else if (eventId.HasValue && objectToUpdate.Action != i.Action || i.ActionOn == DateTime.MinValue)
                {
                    objectToUpdate.Action = i.Action;
                    objectToUpdate.ActionOn = null;

                    var inventoryGoodsEventsList = memmoryInventoryGoodsEvent.Where(x => x.InventoryGood == objectToUpdate.Id);

                    foreach (InventoryGoodsEvents goodsEvent in inventoryGoodsEventsList)
                    {
                        context.InventoryGoodsEvents.DeleteOnSubmit(goodsEvent);

                        var eventToUpdate = goodsEventDictionary[goodsEvent.EventId];

                                eventToUpdate.IsDeleted = true;
                                eventToUpdate.DeletedOn = DateTime.Now;
                                eventToUpdate.DeletedBy = System.Web.HttpContext.Current.User.Identity.Name;                            
                    }

                }
            }
            context.SubmitChanges();
            scope.Complete();
        }

    }

翻开你的单子。无论如何,没有人能够一次处理8000行。页面显示您的列表,提供过滤器帮助您的用户查找内容,这样您将大大减少任何一种方式发送的数据量。

8000行?您应该放弃这种想法,实现某种分页。无论应用程序是什么,向用户显示8000行都是无用的。我使用的是具有分页功能的

不要这样做。没有浏览器会喜欢呈现这么多信息,即使您可以让服务器快速发送。任何显示这么多行的浏览器在客户端上运行速度都会非常慢,滚动会很快,而且通常无法使用

我建议要么对信息进行分页,要么更好地过滤信息,只显示用户感兴趣的内容,任何用户都不可能一次对这么多数据感兴趣

有两种选择:

说服客户分页是必要的,否则性能会受到影响。向他展示其他知名网站是如何做到这一点的。 使用AJAX。只加载屏幕上可以看到的内容,选择一个,当用户滚动时,用AJAX加载缺少的部分。有点像。你也可以尝试一些来避免沉重的负担。
无论您选择什么选项,都可以通过使用helpers来修复视图当前表示的标记。一个可能有帮助的选项是,对于每种类型的下拉列表,您仅以json格式在页面上呈现一次下拉列表的数据项。然后使用javascript在客户机上创建并填充所有select ddl。它将为渲染的每一行保存所有多余的冗余数据传输。您只需存储在生成select时将在每行中选择的值。这只是一个我没有尝试过的想法,这可能会导致客户端也在等待javascript完成它的工作


另外,为了更新数据,是否可以使用ajax并在更改发生时调用每个正确更改的下拉列表?

我遇到的第一个问题是,缓慢的代码在哪里

在服务器处理时间、网络传输时间还是在浏览器中渲染时间


在某些情况下,IE甚至IE8上的大型HTML表速度太慢。使用CSS表格布局:修复此问题可能会有所帮助。

最重要的是,为列表添加页面。也许我也会考虑分页。我很乐意把信息放在页面上,但我做不到,因为那是我的命令。必须一次显示所有信息。。。我知道这是不可取的,但我需要至少让这个过程少一点痛苦…祝你好运。当DOM元素太多时,浏览器会开始呕吐。仅仅因为管理层要求它并不意味着它是可行的。没错。。。但不管我说什么,这些都是要求,我只能遵守。不管你们能说些什么,我都很感激。即使你只是在double foreach上帮忙…@Hallaghan-如果是这样的话,那么就按原样给他们,告诉他们你遇到了性能问题,因为他们需要大量的信息。。。正如老话所说,‘你不能给我的猪涂口红’,他们只是要求我再一次提高性能。这种双重foreach迭代可以改进吗?这是不可能的,因为只有当用户单击Save按钮时,更改才会确定。