title: MVC Add Record to Database
body: <p>Could somebody please help me add a record to a database?</p>
  
  <p>I have created some base elements, but I'm struggling with the code for the AccountController. I would like for a user to enter the values for Stone and Pound via the form, and on posting add a record to the Weight table along with current Id of the logged in user and current date. Here is what I have so far.</p>
  
  <p><strong>AddWeightModel</strong></p>
  
  <pre><code>public class AddWeightModel
  {
      [Required]
      [DataType(DataType.Text)]
      [Display(Name = "Stone")]
      public Nullable&lt;short&gt; Stone { get; set; }
  
      [Required]
      [DataType(DataType.Text)]
      [Display(Name = "Pound")]
      public Nullable&lt;short&gt; Pound { get; set; }
  }
  </code></pre>
  
  <p><strong>WebApplication1Entities</strong></p>
  
  <pre><code>public partial class WebApplication1Entities : DbContext
  {
      public WebApplication1Entities()
          : base("name=WebApplication1Entities")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          throw new UnintentionalCodeFirstException();
      }
  
      public virtual DbSet&lt;Weight&gt; Weights { get; set; }
  }
  </code></pre>
  
  <p><strong>Weight</strong></p>
  
  <pre><code>public partial class Weight
  {
      public int Id { get; set; }
      public string UserId { get; set; }
      public Nullable&lt;short&gt; Stone { get; set; }
      public Nullable&lt;short&gt; Pound { get; set; }
      public Nullable&lt;System.DateTime&gt; Date { get; set; }
  }
  </code></pre>
  
  <p><strong>_UpdatePartial</strong></p>
  
  <pre><code>@using Microsoft.AspNet.Identity
  @model WebApplication1.Models.AddWeightModel
  
  @using (Html.BeginForm("RecordCard", "Account", FormMethod.Post, new { @id = "contact-form", role = "form" }))
  {
      &lt;fieldset&gt;
          @Html.AntiForgeryToken()
          @Html.ValidationSummary()
          &lt;div class="form-div-5"&gt;
              &lt;label&gt;
                  @Html.TextBoxFor(m =&gt; m.Stone, new { @placeholder = "Stone *", @type = "text" })
              &lt;/label&gt;
          &lt;/div&gt;
          &lt;div class="form-div-5"&gt;
              &lt;label&gt;
                  @Html.TextBoxFor(m =&gt; m.Pound, new { @placeholder = "Pound *", @type = "text" })
              &lt;/label&gt;
          &lt;/div&gt;             
          &lt;div class="button-wrapper"&gt;
              &lt;input type="submit" value="Submit" class="button" /&gt;
          &lt;/div&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p><strong>AccountController</strong></p>
  
  <pre><code>public ActionResult RecordCard()
  {
      var UserId = User.Identity.GetUserId();
      var weightModel = from m in db.Weights where m.UserId == UserId select m;
      return View(weightModel);
  }
  
  public ActionResult RecordCard(Weight Model)
  {
      if (ModelState.IsValid)
      {
          using (WebApplication1 db = new WebApplication1())
          {
              Weight weight = new Weight();
              weight.UserId = User.Identity.GetUserId();
              weight.Stone = Model.Stone;
              weight.Pound = Model.Pound;
              weight.Date = System.DateTime.Now;
  
              db.Weights.Add(Model);
              db.SaveChanges();
          }
      }
      return View(Model);
  }
  </code></pre>
  
  <p>Please note that _UpdatePartial is called from RecordCard like so:</p>
  
  <pre><code>@Html.Partial("_WeightPartial", new AddWeightModel()) 
  </code></pre>
  
  <p>And also RecordCard receives an IEnumerable list:</p>
  
  <pre><code>@model IEnumerable&lt;Shedtember.Models.Weight&gt;
  </code></pre>
  
  <p>I need a list of records from the Weight table depending on logged in user to generate a graph.</p>
  
  <p>Just want to add the record and return to the RecordCard page.</p>
  
  <p>Please help, I'm going insane!</p>