我目前正在使用asp.net mvc4。並實施用戶配置文件信息。我的控制器I-e HomeController包含;Asp.Net MVC4'對象引用未設置爲對象的實例'
public ActionResult Resume()
{
using (ProfileInfo profile = new ProfileInfo())
{
return View(profile.ProfileGetInfoForCurrentUser());
}
}
我profileinfo類包含一個方法至極返回類型 'ResumeViewModel';
public ResumeViewModel ProfileGetInfoForCurrentUser()
{
ProfileBase profile = ProfileBase.Create(Membership.GetUser().UserName);
ResumeViewModel resume = new ResumeViewModel();
resume.Email = Membership.GetUser().Email.ToString();
resume.FullName = Membership.GetUser().UserName;
return resume;
}
現在我ResumeViewModel看起來是這樣的;
public class ResumeViewModel
{
public string FullName { get; set; }
public string Email { get; set; }
}
雖然我的觀點是強烈類型的'@model PortfolioMVC4.Models.ResumeViewModel'
。但是,當我運行這個時,我得到以下錯誤;
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
{
Line 14: ProfileBase profile = ProfileBase.Create(Membership.GetUser().UserName);
Line 15: ResumeViewModel resume = new ResumeViewModel();
Line 16: resume.Email = Membership.GetUser().Email.ToString();
Line 17: resume.FullName = Membership.GetUser().UserName;
我收到第15行的錯誤;這基本上是ProfileGetInfoForCurrentUser方法中存在的代碼(如上所示)。 我不知道該怎麼辦? 對此的任何幫助都是可以理解的;
你爲什麼要放置你的視圖模型? – simonlchilds
我應該刪除它嗎? –
行15沒有辦法拋出NullReferenceException。第14行更有可能,也許Membership.GetUser()不返回用戶,因此訪問UserName屬性失敗。 – SpaceBison