简体   繁体   English

如何创建有关谁创建元素的信息的列

[英]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. 如何使用有关谁创建了一个元素的信息来创建列(列CreatedBy)我使用Windows身份验证创建了ASP Net Core 2.0 MVC Web应用程序。 I implemented information about who was modifying last, sucessfuly, but i dont get it with CreatedBy. 我实现了有关谁最后一次修改成功的信息,但我无法通过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, 根据您的评论,我了解到您想根据更新请求更新Modifyby并在创建请求中分配createdby,

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 为此,您应检查是否已分配ID,如果已分配ID,则不是更新请求,否则是创建请求

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET.MVC 5如何显示有关谁创建了元素的信息? - ASP.NET.MVC 5 How can i display information about who created an element? 如何使用令牌获取有关用户的信息 - How can I get information about the user using a token 我怎样才能创建关于这种情况的正则表达式 - How can i create this regex about this condition 如何获取我创建的 WPF 元素的属性 - How can I get properties of a WPF element I have created 如何在按钮按下事件期间获取我创建的信息以显示在我的html页面上? - How can I get information I have created during the button press event to display on my html page? 在 controller 在 asp.net-mvc 中,我如何获取有关用户浏览器的信息? - in a controller in asp.net-mvc how can i get information about the users browser? 如何以编程方式获取有关其他解决方案的信息或进行操作? (Visual Studio,C#) - How can I programmatically get information about or manipulate another solution? (Visual Studio, C#) 如何在c#中赋值后保留有关对象的信息? - How can I persist information about an object after assignment in c#? 使用 c# 如何提取有关本地计算机上存在的硬盘驱动器的信息 - using c# how can I extract information about the hard drives present on the local machine 如何为列创建一个多值? - How can I create a multi value for a column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM