如何将当前用户ID(会话中)名称插入到createdby&的表列中updatedby

时间:2018-01-05 08:45:59

标签: sql asp.net-mvc

我有这样的表......但是我的设计页面只包含一个文本区域,那就是问题答案栏,我的表格就像这样。

public class QuestionAnswer
{
    public int Id { get; set; }

    [Required(ErrorMessage = "Required")]
    [StringLength(1500, MinimumLength = 50, ErrorMessage = "Invalid")]
    public string Question_Answer { get; set; }
    public bool IsAnswer { get; set; }
    public int QuestionId { get; set; }
    public bool IsMarkedAsAnswer { get; set; }
    public int Score { get; set; }
    public DateTime CreatedDateTime { get; set; }
    public DateTime UpdatedDateTime { get; set; }
    public int CreatedBy { get; set; }
    public int UpdatedBy { get; set; }
}'

我的控制器就像这样,然后如何通过在当前用户ID的帮助下按列更新创建的insret?

[HttpGet]
    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(UserAccountModel model)
    {
        StackProvider provider = new StackProvider();
        model.UserAccount = provider.GetUseraccountByEmailPassword(model.UserAccount.Email, model.UserAccount.Password);

        if(!string.IsNullOrEmpty(model.UserAccount.Exception))
        {
            ModelState.AddModelError("UserAccount.Email", model.UserAccount.Exception);
            return View(model);
        }
        else
        {
            Session["UserId"] = model.UserAccount.UserId; 
             Session["UserName"] = model.UserAccount.UserName;
            Session["Email"] = model.UserAccount.Email;
            return RedirectToAction("Homepage", "QuestionAnswer");
        }
    }


}

1 个答案:

答案 0 :(得分:1)

在保存QuestionAnswer模型的操作中,例如:

[HttpPost]
public ActionResult Save(QuestionAnswer model)
{
    ...
    var userId = (int)Session["UserID"];
    model.CreatedBy = userId;
    model.UpdatedBy = userId;
    ...
}