简体   繁体   中英

How can i create column with information about who Created an element

Can you help me. How can i create column with information about who Created an element(column CreatedBy) I created asp net core 2.0 MVC Web-Application with Windows authentication. I implemented information about who was modifying last, sucessfuly, but i dont get it with CreatedBy. My model is

 public class TestModel
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public string CreatedBy { get; set; }
        [DataType(DataType.Date)]
        public DateTime Created { get;}
        public TestModel()
        {
            Created = DateTime.Now;
        }
        public string ModifiedBy { get; set; }
        public DateTime Modified { get; set; }
    }

My controller of Create

public async Task<IActionResult> Create([Bind("Id,Description")] TestModel testModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(testModel);
                testModel.CreatedBy = this.User.Identity.Name;
               // testModel.ModifiedBy = this.User.Identity.Name;
                testModel.Modified = DateTime.Now;
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(testModel);
        }

Edit controller

public async Task<IActionResult> Edit(int id, [Bind("Id,Description")] TestModel KestModel)
        {
            if (id != KestModel.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {

                  await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TestModelExists(KestModel.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }


            return View();
        }

As per your comment I understood that you want to update modifyby on update request and assign createdby at the create request,

For this you should check the Id which is already assign or not, If id is already assign than it is update request else it is create request

Try below code changes

public async Task<IActionResult> Create([Bind("Id,Description")] TestModel testModel)
{
    if (ModelState.IsValid)
    {                   
        if(testModel.Id > 0){
            // the entity is already created and it is modify request
            _context.Entry(testModel).State = EntityState.Modified;

            testModel.ModifiedBy = this.User.Identity.Name;
            testModel.Modified = DateTime.Now;
        }
        else{
            // it is create request
            _context.Entry(testModel).State = EntityState.Added;            

            testModel.CreatedBy = this.User.Identity.Name;
            testModel.Created = DateTime.Now;
        }            

        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View(testModel);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM