这是我的ViewModel:
public class PlayerViewModel
{
PlayerRepository repo = new PlayerRepository();
public Player Player { get; set; }
public int SelectedUserID { get; set; }
public SelectList Users { get; set; }
public PlayerViewModel()
{
Player = new Player();
}
public PlayerViewModel(Player player)
{
Player = player;
SelectedUserID = 0;
}
}
这是在我的控制器中(用于编辑):
[Authorize]
public ActionResult Upravit(int id)
{
var player = repo.Retrieve(id);
var playerView = new PlayerViewModel(player);
playerView.Users = new SelectList(repo.GetUsers(), "UserID", "UserName");
return View(playerView);
}
[Authorize,HttpPost]
public ActionResult Upravit(int id, PlayerViewModel playerView)
{
if (ModelState.IsValid)
{
playerView.Player.User = repo.GetUserByID(playerView.SelectedUserID);
repo.Save(playerView.Player);
return RedirectToAction("Podrobnosti", new { id = playerView.Player.PlayerID });
}
return View(playerView);
}
和回购:
public User GetUserByID(int id)
{
return db.Users.Find(id);
}
public int Save(Player player)
{
db.Entry(player).State = player.PlayerID == 0 ?
EntityState.Added : EntityState.Modified;
db.SaveChanges();
return player.PlayerID;
}
我的问题是我进入httppost有效的viewmodel,我将User添加到播放器(它是自定义类,在播放器类中是虚拟用户用户),在调试中它看起来一切正常(播放器包含正确的用户)但是下次我在其他视图中尝试检索播放器它的属性,User为null。我在数据库中检查了它,并且在UserID列中没有int。所以也许有人可以帮助我在哪里出问题?为什么不保存?
修改 我将此代码添加到我的存储库中:
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
db.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
并将控制器更改为:
[Authorize,HttpPost]
public ActionResult Upravit(int id, PlayerViewModel playerView)
{
if (ModelState.IsValid)
{
using (PlayerRepository playerRepository = new PlayerRepository())
{
playerView.Player.User = playerRepository.GetUserByID(playerView.SelectedUserID);
playerRepository.Save(playerView.Player);
return RedirectToAction("Podrobnosti", new { id = playerView.Player.PlayerID });
}
}
return View(playerView);
}
但没有帮助。
EDIT2 我的播放器和用户类:
public class User
{
public User()
{
}
public User(int userId, string userName, string firstName, string surname, string password, string email)
{
UserID = userId;
UserName = userName;
Name = firstName;
Surname = surname;
Password = password;
Email = email;
IsActivated = false;
}
[HiddenInput(DisplayValue = false)]
public int UserID { get; set; }
[Display(Name="Uživatelské jméno")]
[Required(ErrorMessage = "Je potřeba vyplnit uživatelské jméno.")]
[StringLength(20)]
public string UserName { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
[Display(Name = "Heslo")]
[DataType(DataType.Password)]
[Required(ErrorMessage = "Je potřeba vyplnit heslo.")]
[StringLength(30)]
public string Password { get; set; }
[Display(Name = "E-mail")]
[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Je potřeba vyplnit Váš e-mail.")]
[StringLength(30)]
public string Email { get; set; }
public IIdentity Identity { get; set; }
public string ImagePath { get; set; }
public virtual ICollection<Article> Articles { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual ICollection<Topic> Topics { get; set; }
public int RoleId { get; set; }
public virtual Role Role { get; set; }
public bool IsActivated { get; set; }
public string EmailKey { get; set; }
}
}
public class Player
{
[Key]
public int PlayerID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Surname { get; set; }
public string PhotoUrl { get; set; }
public string Post { get; set; }
public virtual Team Team { get; set; }
public virtual User User { get; set; }
}
我使用repo对Controller中的每个方法进行了添加,并在控制器上添加了Dispose方法,现在我在显示数据方面遇到了问题。例如,当我尝试显示Player.User.UserName(我已经手动向玩家添加了一些用户)时,它说实例ObjectDataContext是dispose并且无法使用。
答案 0 :(得分:0)
似乎有必要处置ObjectContext
;否则每次都有不同的ObjectContext
个实例,在上下文的对象之间进行查询时会遇到问题。
为您的Repository类实现IDisposable:为底层ObjectContext
类实例调用Dispose方法。
客户代码:
using (PlayerRepository playerRepository = new PlayerRepository())
{
// Perform queries, etc.
}
<强>更新强>
为了简化存储库的处理,只需覆盖Dispose
派生类的Controller
方法:在此处为存储库调用Dispose
。 ASP.NET MVC框架将在请求结束时自动调用Controller.Dispose()
。
<强>更新强>
有必要定义名为<entity>Id
的属性来定义关系。
public class Player
{
// Having a property called <entity>Id defines a relationship.
public int UserId { get; set; }
public virtual User User { get; set; }
}
希望这有帮助。