ASP.NET.MVC 5如何显示有关谁创建了元素的信息?

时间:2018-06-28 10:15:48

标签: c# asp.net asp.net-mvc-5

使用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);
        }

1 个答案:

答案 0 :(得分:1)

重写您的Create操作,如下所示:

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);
    }

对于“编辑”操作,请执行与对“创建”操作相同的操作。 希望有帮助!