0

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.

umki
  • 769
  • 13
  • 31

1 Answers1

0

You are going to have to get the CrDate and CrUser to the model creation or LogBasic function no matter what. If you don't when you update they will be nulled out as no value is set in the model. I'd recommend storing them as hidden fields and binding them as normal. That way you don't have to make two trips to the DB to do an update.

EDIT: To make this a bit easier you could add this method to your BaseModel class. It would output hidden fields to bind against. I've made some assumptions (like UserIds are all positive integers).

public MvcHtmlString BaseModelHiddenFields()
{
    StringBuilder outputString = new StringBuilder();
    if(CrDate != null && CrDate.HasValue)
    {
        sb.AppendFormat("<input type=\"hidden\" name\="CrDate\" value=\"{0}\">", CrDate.Value);
    }
    if(CrUser > 0)
    {
        sb.AppendFormat("<input type=\"hidden\" name\="CrUser\" value=\"{0}\">", CrUser);
    }
    if(MdDate != null && MdDate.HasValue)
    {
        sb.AppendFormat("<input type=\"hidden\" name\="MdDate\" value=\"{0}\">", MdDate.Value);
    }
    if(MdUser > 0)
    {
        sb.AppendFormat("<input type=\"hidden\" name\="MdUser\" value=\"{0}\">", MdUser);
    }

    return MvcHtmlString.Create(sb.ToString());
}
Josh
  • 1,724
  • 13
  • 15
  • Thanks for your reply, but when i need to add one more parameter to the base model, i need to put input hidden type to all of my Edit views, right ? – umki Jul 11 '14 at 15:46
  • That's correct. You might want to create a method on your base model that outputs all the hidden fields. That way you can just call that in the view. Makes it easier and less error prone than doing it from scratch. – Josh Jul 11 '14 at 15:51
  • Yes, my question is about that, but i cant create that method, could you please help me ? – umki Jul 12 '14 at 08:23