2013-08-17 114 views
0

我在ASP.NET MVC4 Web應用程序中定義了一個Exercise實體。 我使用默認的AccountModels.cs類的表單身份驗證。在ASP.NET MVC4中保存用戶和實體之間的關係

我有課,它看起來像

public class Exercise 
    { 

     private DateTime _DateCreated = DateTime.Now; 
     private UserProfile _Teacher; 
     public int Id{ get; set; } 
     public string Question { get; set; } 
     public int Anwser { get; set; } 
     public string Category { get; set; } 
     public int maxNbrOfAttempts { get; set; } 
     public string Hints { get; set; } 
     public virtual ICollection<Quiz> Quizzes { get; set; } 

     public DateTime Date 
     { 
      get { return _DateCreated; } 
      set { _DateCreated = value; } 
     } 

     public UserProfile Author 
     { 
      get { return _Teacher; } 
      set { _Teacher = value; } 
     } 

    } 

我是否正確地使用用戶配置的運動與登錄的用戶之間的鏈接? 如何在我的控制器中獲取當前的UserProfile?

回答

0

改變這樣的:

public class Exercise 
{ 
    public Exercise() 
    { 
     this.Date = DateTime.Now; 
     this.Author = User.Identity.Name; //Write this line if you want to set 
              //the currently logged in user as the Author 
    public int Id{ get; set; } 
    public string Question { get; set; } 
    public int Anwser { get; set; } 
    public string Category { get; set; } 
    public int maxNbrOfAttempts { get; set; } 
    public string Hints { get; set; } 

    public virtual ICollection<Quiz> Quizzes { get; set; } 

    public virtual DateTime Date { get; set; }   
    public virtual UserProfile Author { get; set; }   
}