I have a basemodel as below, and i am using this model to get/set created and modified date per model.
public class BaseModel
{
[ForeignKey("CrUser")]
public ApplicationUser UserCr { get; set; }
public string CrUser { get; set; }
public DateTime? MdDate { get; set; }
[ForeignKey("MdUser")]
public ApplicationUser UserMd { get; set; }
public void LogBasic()
{
if (this.CrDate == null)
{
this.CrDate = System.DateTime.Now;
this.CrUser = HttpContext.Current.User.Identity.GetUserId();
}
else
{
this.MdDate = System.DateTime.Now;
this.MdUser = HttpContext.Current.User.Identity.GetUserId();
}
}
}
While creating new entry there is no problem but while updating a model, i could not set the MdUser. I know the reason is, CrUser and CrDate is empty, as you can see below method, they are not binding.
Here is the my one of edit method
public ActionResult Edit([Bind(Include = "Id,Name,FullName,Address,TaxNumber,TaxOffice,Phone1,Phone2,Email,CompanyType")] CrmCompany crmcompany)
{
if (ModelState.IsValid)
{
crmcompany.LogBasic();
db.Entry(crmcompany).State = EntityState.Modified;
db.SaveChanges();
}
}
To solve the problem with my way, i need to pass CrDate and CrUser, or i need to get from database for every Edit Method.
How can i solve this problem in my LogBasic() method. If i can access to DB and get the Crdate and Cruser in LogBasic() method, i can easily solve the problem but, any model can call the LogBasic() method.