8

我有 mvc 4 母版页,我想在页面顶部显示用户特定的菜单我正在检索用户数据

@using Agenda_MVC.Engine
@{

    List<Format> ContactEvents = Agenda_MVC.Classes.Utility.GetContactEventFormats(User.Identity.Name);
}

但是当我遍历列表时它会抛出空异常,在设置断点时列表不为空它有项目。

@foreach (var ContactEvent in ContactEvents)
                {
                    <div class="@(ContactEvent.EventId == ViewContext.RouteData.Values["id"].ToString() ? "StaticMenuItemSelected" : "StaticMenuItem")">
                       @Html.ActionLink(ContactEvent.Name.Length > 15 ? ContactEvent.Name.Substring(0, 15) + "..." : ContactEvent.Name, "Agenda", "Portal", new { id = ContactEvent.EventId }, new { @title = ContactEvent.Name })
                    </div>    
                }

在此处输入图像描述

我不知道我做错了什么。


方法代码如下我正在从 Web 服务中检索它

public static List<Format> GetContactEventFormats(string ContactId)
{
    //List<Format> EventFormats = HttpContext.Current.Cache["EventFormats" + ContactId] as List<Format>;
    List<Format> EventFormats = HttpContext.Current.Session["EventFormats" + ContactId] as List<Format>;

    if (EventFormats == null)
    {
        EngineClient EngClient = Params.GetEngineClient();
        EventFormats = EngClient.GetContactEventFormats(ContactId);

        if (EventFormats != null && EventFormats.Count > 0)
        {
            //HttpContext.Current.Cache.Insert("EventFormats" + ContactId, EventFormats, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero);
            HttpContext.Current.Session["EventFormats" + ContactId] = EventFormats;
        }
    }

    return EventFormats == null ? new List<Format>() : EventFormats;
}
4

2 回答 2

4

我假设ContactEvent.NameEventId为空

debugger您可以通过在将 ContactEvent.Name 分配给值之前指定条件来检查这两个值是否为空ActionLink

<div class="@(ContactEvent.EventId == ViewContext.RouteData.Values["id"].ToString() ? "StaticMenuItemSelected" : "StaticMenuItem")">

                @if (ContactEvent.Name != null) {
                   @Html.ActionLink(ContactEvent.Name.Length > 15 ? ContactEvent.Name.Substring(0, 15) + "..." : ContactEvent.Name, "Agenda", "Portal", new { id = ContactEvent.EventId }, new { @title = ContactEvent.Name })
                }
                </div>   
于 2013-04-24T12:57:11.450 回答
0

在调试期间查看 List 中的 ContactEvent 对象。

也许(可能)一个或多个字段,例如:ContactEvent.EventIdContactEvent.Name为空。

于 2013-04-24T12:59:24.303 回答