3

我们有一个 MVC 4 Web 应用程序。我使用 miniprofiler 检查了它需要多长时间,它表明渲染具有三个部分视图的视图需要很长时间。我尝试将部分视图中的信息直接移动到页面上,但这在时间上没有任何影响。我应该怎么做才能减少渲染时间?关于我应该从哪里开始寻找的任何提示?如您所见,渲染需要 48 秒。

                                         **| duration | from start(ms)**
|http://localhost:61380/CaseContacts     | 653.7    | +0.0 
|Controller: CaseController.Contacts     | 11.9     | +641.1 
|    Contacts view - not an ajax request | 0.3      | +651.7
|        populating view model           | 0.0      | +652.1
|    getting list of contacts            | 374.4    | +652.1
|    Find: Contacts                      | 499.8    | +1033.6
|    Render: Contacts                    | 48400.8  | +1535.2

**| client event           |duration(ms)| from start(ms)**
|Request start           |            | +2.0
|Response                |   1.0      | +52245.0
|Dom loading             |            | +52247.0
|scripts                 | 390.0      | +52315.0 
|Dom Content Loaded Event| 29.0       | +52707.0
|Dom Interactive         |            | + 52707.0
|Load Event              |            | + 52760.0
|Dom Complete            |            | + 52760.0

更新:

public IQueryable<T> FindQueryable(Expression<Func<T, bool>> predicate, Func<T, Object> orderBy)
    {
        return this.db.GetAll<T>().Where(predicate).OrderBy(orderBy).AsQueryable<T>();
    }
public class ContactRepository : Repository<USRContact>, IContactRepository<USRContact>
{
    public IList<ContactNameViewModel> GetAll(int fieldOfficeID, string searchText = "")
    {
        if (searchText.Length > 0)
            return Mapper.Map<IList<USRContact>, IList<ContactNameViewModel>>(this.FindQueryable(x => (x.FieldOfficeID == fieldOfficeID) &&
            (x.FormalName.Contains(searchText) || x.PreferredName.Contains(searchText) || x.Alias.Contains(searchText) || x.Pseudonym.Contains(searchText)), x => (x.STMGender.Gender)).ToList());
        else
            return Mapper.Map<IList<USRContact>, IList<ContactNameViewModel>>(this.FindQueryable(x => (x.FieldOfficeID == fieldOfficeID)).ToList());
    }

然后我的控制器将结果加载到视图模型上,然后:

@Html.Partial("~/Views/Shared/Contacts/ContactNameList.cshtml", Model.Contacts)

部分视图包含以下代码:

@model IList<Casenator.Web.Models.Contact.ContactNameViewModel>
<ul id="NameList" style="margin-left:0">
    @foreach (var item in Model)
    {
        @*<li>@Html.RouteLink(@item.FullName, "Contacts", new { id = item.ContactID })</li>*@
        <li>@Ajax.RouteLink(item.FullName, "Contacts", new { id = item.ContactID }, new AjaxOptions { UpdateTargetId = "BasicInfo", OnSuccess="InitializeComboBoxes", OnBegin="LoadContact(" + item.ContactID + ")" }) </li>
    }
</ul>

任何帮助将不胜感激!谢谢,萨弗里斯

4

1 回答 1

1

您从查询返回的内容很重要。如果您要返回IEnumerable,最好返回IQueryable。您可能会看到这个问题,它可能会对您的情况有所帮助

我不认为automapper像工具旨在映射许多记录。您可以尝试手动映射对象

于 2013-03-04T21:47:38.077 回答