由於某種原因,我的ASP.NET MVC3網頁一直拋出我NullReferenceExceptions。奇怪的是,我不可能找出問題的根源。無論我在我看來寫的第一段代碼(在這種情況下,我的foreach)據說會導致這個錯誤。NullReferenceException不管我在我的視圖中做什麼
我的看法是這樣的:
<%@ 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個不同的控制器使用方法:
這是一個將數據放入視圖模型對象中:
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了未處理的用戶代碼
消息=未將對象引用設置爲對象的實例。
源= App_Web_nvi5brwn堆棧跟蹤: 在ASP.views_home_index_aspx .__ RenderContent3(HtmlTextWriter的__w,控制parameterContainer)在C:\用戶\ AronChan \桌面\ Dropbox的\ SkyLearn \ Skylearn網站(阿隆 sikkerhedskopi)\ SkyLearn \查看\首頁\指標.aspx:line 8 at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI.Control.Render( HtmlTextWriter的作家) 在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter的作家的ControlAdapter適配器) 在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家的ControlAdapter適配器) 在System.Web.UI.Control。 RenderControl(HtmlTextWriter的作家) 在ASP.views_shared_site_master._ 渲染 _control1(HtmlTextWriter的__w, 控制parameterContainer)在 C:\用戶\ AronChan \桌面\ Dropbox的\ SkyLearn \ Skylearn網站(阿隆 sikkerhedskopi)\ SkyLearn \瀏覽\ Shared \ Site.Master:line 34 at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI。 Control.Render(HtmlTextWriter的作家) 在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter的作家的ControlAdapter適配器) 在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家,ControlAdap在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家) 之三適配器) 在System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter的作家,ICollection的孩子) 在System.Web.UI.Control.RenderChildren(HtmlTextWriter的作家) 在System.Web.Mvc.ViewPage.Render(HtmlTextWriter的作家) 在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter的作家的ControlAdapter適配器) 在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家,的ControlAdapter適配器) 在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家) 在System.Web.UI.Page。ProcessRequestMain(布爾includeStagesBeforeAsyncPoint,布爾includeStagesAfterAsyncPoint)
的InnerException:
我已經嘗試了所有我能想到的。請幫助我xD
你是絕對正確的,是一位真正的紳士先生!謝謝 :) – AronChan 2012-04-19 21:21:16