如何为每个不同的用户创建不同的数据?
How to create different data with each different user?
我正在使用 asp.net entity framework 和 asp.net 身份,我想做的是注册一些帐户并使用 user#1(agentmi6) 创建一些数据,例如:
然后使用用户#2(john) 登录并使用该用户制作一些数据:
所以我的主要问题是如何让用户只查看他们创建的数据。
在我的电影模型中,我添加了与应用程序用户的关系
public class Movie
{
[Key]
public int MovieId { get; set; }
public string Name { get; set; }
public string Year { get; set; }
public string Genre{ get; set; }
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
然后在 MoviesController 中添加此查询以获取当前注册用户创建的所有电影:
public ActionResult Index()
{
var currentUser = User.Identity.GetUserId();
var movies = db.Movies.Where(x => x.ApplicationUserID == currentUser);
return View(movies);
}
并且在我创建电影时在控制器的创建操作中将当前用户添加到该电影
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
movie.ApplicationUserID = User.Identity.GetUserId();
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
在 MoviesController 的所有视图中,我删除了应用程序用户 html,因为我不想在索引和创建电影时显示。
最后我得到了我想要的东西。
用用户a@登录a.com并制作了一个只有他能看到的电影,
第二个用户 v@v.com 只能看到他的电影。
我正在使用 asp.net entity framework 和 asp.net 身份,我想做的是注册一些帐户并使用 user#1(agentmi6) 创建一些数据,例如:
然后使用用户#2(john) 登录并使用该用户制作一些数据:
所以我的主要问题是如何让用户只查看他们创建的数据。
在我的电影模型中,我添加了与应用程序用户的关系
public class Movie
{
[Key]
public int MovieId { get; set; }
public string Name { get; set; }
public string Year { get; set; }
public string Genre{ get; set; }
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
然后在 MoviesController 中添加此查询以获取当前注册用户创建的所有电影:
public ActionResult Index()
{
var currentUser = User.Identity.GetUserId();
var movies = db.Movies.Where(x => x.ApplicationUserID == currentUser);
return View(movies);
}
并且在我创建电影时在控制器的创建操作中将当前用户添加到该电影
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
movie.ApplicationUserID = User.Identity.GetUserId();
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
在 MoviesController 的所有视图中,我删除了应用程序用户 html,因为我不想在索引和创建电影时显示。
最后我得到了我想要的东西。
用用户a@登录a.com并制作了一个只有他能看到的电影,
第二个用户 v@v.com 只能看到他的电影。