出于某种原因,我的 ASP.NET MVC3 网页不断向我抛出 NullReferenceExceptions。奇怪的是,我无法确定问题的根源。无论我在我的视图中写什么,据说第一段代码(在本例中是我的 foreach)都会导致此错误。
我的观点是这样的:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SkyLearn.Models.StartpageDataViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Skylearn - Startside
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="SideContent" runat="server">
<%foreach (var category in Model.CurrentUsersCategories)
{%>
<div class="homecategory"><div class="homecategoryicon"></div><%: Html.DisplayFor(Title => category.Title)%></div>
<%}%>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%: ViewBag.Message %>
</asp:Content>
这是视图模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using SkyLearn.Areas.Categories.Models;
using SkyLearn.Areas.Categories.Controllers;
namespace SkyLearn.Models
{
public class StartpageDataViewModel
{
public List<Category> CurrentUsersCategories { get; set; }
public StartpageDataViewModel()
{
CurrentUsersCategories = new List<Category>();
}
}
}
最后是我从 2 个不同的控制器中使用的方法:
这是将数据放入 viewmodel 对象的那个:
public ActionResult Index()
{
if (Request.IsAuthenticated)
{
StartpageDataViewModel model = new StartpageDataViewModel();
MembershipUser currentuser = Membership.GetUser();
List<Category> categories = new List<Category>();
categories = accountcontroller.getCategoriesByRoles((Guid)currentuser.ProviderUserKey);
foreach (Category category in categories)
{
model.CurrentUsersCategories.Add(category);
}
ViewBag.Message = "Velkommen til Skylearn Video Tutorials " + currentuser.UserName + "!";
return View(model);
}
else
{
ViewBag.Message = "Log venligst ind for at benytte Skylearn Video Tutorials";
return View();
}
}
这是获取一些数据的那个:
public List<Category> getCategoriesByRoles(Guid userid)
{
List<Category> categoriesbyrole = new List<Category>();
MembershipUser user = Membership.GetUser(userid);
string[] roles = Roles.GetRolesForUser(user.UserName);
List<Category> categories = categorycontroller.getCategories();
foreach (string role in roles)
{
foreach (Category category in categories)
{
if (role == category.Title)
{
categoriesbyrole.Add(category);
}
}
}
return categoriesbyrole;
}
在我看来,无论我做什么,我都会在 foreach 上得到当前错误:
System.NullReferenceException 未被用户代码处理
Message=Object 引用未设置为对象的实例。
Source=App_Web_nvi5brwn StackTrace: at ASP.views_home_index_aspx.__RenderContent3(HtmlTextWriter __w, Control parameterContainer) in c:\Users\AronChan\Desktop\Dropbox\SkyLearn\Skylearn Website (Aron sikkerhedskopi)\SkyLearn\Views\Home\Index.aspx:line 8 在 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 在 System.Web.UI.Control.Render(HtmlTextWriter writer) 在 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 在 System.Web .UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at ASP.views_shared_site_master._使成为_control1(HtmlTextWriter __w, Control parameterContainer) 在 c:\Users\AronChan\Desktop\Dropbox\SkyLearn\Skylearn 网站 (Aron sikkerhedskopi)\SkyLearn\Views\Shared\Site.Master:System.Web.UI.Control 的第 34 行。 RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI.Control.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter 适配器)在 System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter 适配器)在 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)在 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children ) 在 System.Web.UI.Control。RenderChildren(HtmlTextWriter writer) at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter 适配器)在 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)System.Web.UI.Page.ProcessRequestMain 的 RenderControl(HtmlTextWriter writer)(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)System.Web.UI.Page.ProcessRequestMain 的 RenderControl(HtmlTextWriter writer)(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)
内部异常:
我已经尝试了我能想到的一切。请帮帮我xD