title: How to do patch update a model using ASP.net MVC, Entity Framework and Razor
body: <p>I managed to create the basic CRUD operations using asp.net MVC</p>
  
  <p>But I had a problem with regards to my field <code>Status</code>, <code>CreatedOn</code> and <code>CreatedBy</code>.</p>
  
  <p>I want these fields to be populated during the Create Action and is not updated during the Edit Action but I don't want to use @HiddenFor for these fields because you can edit the DOM and still update these information before a POST request.</p>
  
  <p>So basically what I want is to Patch Update my model with whatever data sent in the POST request. So I don't have to worry about the fields I don't want being modified are being changed in the Edit Action.</p>
  
  <p>Here is my code for the model and action. </p>
  
  <p>Model </p>
  
  <pre><code>[Table("Account")]
  public class Account
  {
      #region Properties
  
      [Key]
      public int AccountID { get; set; }
  
      [Required]
      [StringLength(50)]
      [DisplayName("Account Name")]
      public string Name { get; set; }
  
      [Required]
      [DisplayName("Time Offset")]
      [Range(-24,24)]
      public decimal TimeOffset { get; set; }
  
      [StringLength(20)]
      [DisplayName("Status")]
      public string Status { get; set; }
  
      public DateTime CreatedOn { get; set; }
  
      public int CreatedBy { get; set; }
  
      public DateTime? ModifiedOn { get; set; }
  
      public int ModifiedBy { get; set; }
  
      #endregion
  }
  
  public class DbAccount : DbContext
  {
      public DbSet&lt;Account&gt; Accounts { get; set; }
  }
  </code></pre>
  
  <p>Controller Action for Create</p>
  
  <pre><code>[HttpPost]
      public ActionResult Create(Account account)
      {
          if (ModelState.IsValid)
          {
              account.Status = "ENABLED";
              account.CreatedOn = DateTime.Now;
              account.CreatedBy = 1;
              db.Accounts.Add(account);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          return View(account);
      }
  </code></pre>
  
  <p>Controller Action for Edit</p>
  
  <pre><code>[HttpPost]
      public ActionResult Edit(Account account)
      {
          if (ModelState.IsValid)
          {
              account.ModifiedOn = DateTime.Now;
              account.ModifiedBy = 1;
              db.Entry(account).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(account);
      }
  </code></pre>
  
  <p>How can I modify the model without affecting some specific properties specifically <code>CreatedOn</code> and <code>CreatedBy</code>, I want these properties to preserve their original value during Edit Action. I don't want to use Hidden Fields to store them. With my existing solution I used Hidden Fields but it can be modified in the DOM before POST request so it's not secure.</p>