[英]ASP.NET.MVC 5 How can i display information about who created an element?
使用Windows身份驗證創建了一個應用程序。 我需要您的建議,我該如何顯示有關元素的創建者(或被其修改)以及何時(或何時更改)的信息?
我的模特是
Public class Movie
{
public int Id {get;set;}
public string Text{get;set;}
public datetime Created{get;set;}
public string CreatedBy {get;set;}
public datetime Modified{get;set;}
public string ModifiedBy {get;set;}
}
控制器是
public ActionResult Create()
{
return View();
}
// POST: Movies/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Text,Created,CreatedBy,Modified,ModifiedBy")] Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
重寫您的創建操作,如下所示:
public ActionResult Create()
{
return View();
}
// POST: Movies/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Text")] Movie movie)
{
movie.CreatedBy = HttpContext.Current.User;
movie.Created = DateTime.Now;
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
實現與“創建”操作相同的“編輯”操作。 希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.