title: Posting data to the database from mvc form
body: <p>I am trying to save contact data from an MVC form to a sql database. I am also saving the data to an XML document. The XML document is created correctly with the correct data, but I am not getting the data to the database. Any help would be greatly appreciated.</p>
  
  <p>Controller handling Contact data:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Web;
  using System.IO;
  using System.Web.Mvc;
  using TestProject.Models;
  using System.Xml.Serialization;
  using System.Configuration;
  using System.Windows.Forms;
  using Rabbit.Bootstrap;
  
  namespace TestProject.Controllers
  {
      public class ContactsController : Controller
      {
          [HttpGet]
          public ActionResult Index()
          {
              Contact contact = new Contact();
  
              return View(contact);
          }
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Index(Contact c)
          {
              if (ModelState.IsValid)
              {
                  string ContactFileName = Path.GetFileName(String.Format("{0} {1}.xml", c.LastName, c.FirstName));
                  ContactFileName = (@"C:\Users\kevin.schultz\TestDocuments\" + ContactFileName);
                  if (System.IO.File.Exists(ContactFileName))
                  {
                      MessageBox.Show("The file already exists. A number will be added to create a unique file name", "Important", MessageBoxButtons.OK,
                      MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
  
                      int i = 0;
                      while (System.IO.File.Exists(ContactFileName))
                      {
                          ContactFileName = String.Format("{0} - {1}", ContactFileName, i.ToString());
                          i++;
                      }
                  }
  
                  XmlSerializer ser = new XmlSerializer(c.GetType());
                  StreamWriter myWriter = new StreamWriter(ContactFileName);
                  ser.Serialize(myWriter, c);
                  myWriter.Close();
                  return View("ContactSuccess");
  
              }
              return View("ContactError");
          }
  
          [HttpGet]
          public ActionResult Save()
          {
              return View();
          }
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Save (Contact s)
          {
              if (ModelState.IsValid)
              {
                  try
                  {
                      using (ContactDataEntities CustData = new ContactDataEntities())
                      {
                          CustData.dbContacts.Add(
                              new dbContact()
                              {
                                  FirstName = s.FirstName,
                                  LastName = s.LastName,
                                  PhoneHome = s.PhoneHome,
                                  PhoneCell = s.PhoneCell,
                                  Email = s.Email,
                                  Address = s.Address,
                                  City = s.City,
                                  State = s.State,
                                  ZipCode = s.ZipCode,
                              });
                          CustData.SaveChanges();
  
                          return View("ContactSuccess");
                      }
                  }
                  catch (Exception ex)
                  {
                      return View("ContactError");
                  }
              }
  
              else
              {
                  MessageBox.Show("You are missing data, please ensure all fields are correct.", "Important", MessageBoxButtons.OK,
                      MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
              }
              return View();
          }
      }
  }
  </code></pre>
  
  <p>Database</p>
  
  <pre><code>CREATE TABLE [dbo].[Table] (
      [Id]        INT  NOT NULL,
      [FirstName] TEXT NOT NULL,
      [LastName]  TEXT NOT NULL,
      [PhoneHome] TEXT NOT NULL,
      [PhoneCell] TEXT NOT NULL,
      [Email]     TEXT NOT NULL,
      [Address]   TEXT NOT NULL,
      [City]      TEXT NOT NULL,
      [State]     TEXT NOT NULL,
      [ZipCode]   TEXT NOT NULL,
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel;
  
  namespace TestProject.Models
  {
  
          public class Contact 
          {
              [Required]
              [DisplayFormat(ConvertEmptyStringToNull = false)]
              [StringLength(20, MinimumLength = 2, ErrorMessage = "Enter A Valid First Name")]
              public string FirstName { get; set; }
  
              [Required(ErrorMessage = "Enter A Valid Last Name")]
              [DisplayFormat(ConvertEmptyStringToNull = false)]
              [StringLength(20, MinimumLength = 2)]
              public string LastName { get; set; }
  
              [Required(ErrorMessage = "Enter A Valid Home Phone Number including area code")]
              [DataType(DataType.PhoneNumber)]
              public string PhoneHome { get; set; }
  
              [Required(ErrorMessage = "Enter A Valid Cell Phone Number")]
              [DataType(DataType.PhoneNumber)]
              public string PhoneCell { get; set; }
  
              [Required(ErrorMessage = "Enter A Valid Email Address")]
              [DataType(DataType.EmailAddress)]
              public string Email { get; set; }
  
              [Required(ErrorMessage = "Enter A Valid Street Address")]
              [DisplayFormat(ConvertEmptyStringToNull = false)]
              [StringLength(120, MinimumLength = 4)]
              public string Address { get; set; }
  
              [Required(ErrorMessage = "Enter A Valid City Name")]
              [DisplayFormat(ConvertEmptyStringToNull = false)]
              [StringLength(30, MinimumLength = 2)]
              public string City { get; set; }
  
              [Required(ErrorMessage = "Enter A Valid State Abbreviation")]
              [DisplayFormat(ConvertEmptyStringToNull = false)]
              [StringLength(20, MinimumLength = 2)]
              public string State { get; set; }
  
              [Required(ErrorMessage = "Enter A Valid 5 Digit Zip Code")]
              [DataType(DataType.PostalCode)]
              public string ZipCode { get; set; }
          }
  
  
      }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@model TestProject.Models.Contact
  
  @{
      ViewBag.Title = "Contact Page";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h4&gt;Company Contact Form&lt;/h4&gt;
  &lt;br /&gt;
  @using (Html.BeginForm())
  { 
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
      &lt;form class="form-primary"&gt;
          &lt;fieldset class="form-group"&gt;
              &lt;div class="form-label"&gt;
                  @Html.LabelFor(model =&gt; model.FirstName, "First Name", new { style = "display:inline;" })
              &lt;/div&gt;
              &lt;div class="form.field"&gt;
                  @Html.EditorFor(model =&gt; model.FirstName, new { placeholder = "First Name" })
                  @Html.ValidationMessageFor(model =&gt; model.FirstName)
              &lt;/div&gt;
  
          &lt;div class="form-label"&gt;
              @Html.LabelFor(model =&gt; model.LastName, "Last Name", new { style = "display:inline;" })
          &lt;/div&gt;
  
          &lt;div class="form.field"&gt;
              @Html.EditorFor(model =&gt; model.LastName)
              @Html.ValidationMessageFor(model =&gt; model.LastName)
          &lt;/div&gt;
  
          &lt;div class="form-label"&gt;
              @Html.LabelFor(model =&gt; model.PhoneHome, "Home Phone", new { style = "display:inline;" })
          &lt;/div&gt;
  
          &lt;div class="form.field"&gt;
              @Html.EditorFor(model =&gt; model.PhoneHome)
              @Html.ValidationMessageFor(model =&gt; model.PhoneHome)
          &lt;/div&gt;
  
          &lt;div class="form-label"&gt;
              @Html.LabelFor(model =&gt; model.PhoneCell, "Cell Phone", new { style = "display:inline;" })
          &lt;/div&gt;
  
          &lt;div class="form.field"&gt;
              @Html.EditorFor(model =&gt; model.PhoneCell)
              @Html.ValidationMessageFor(model =&gt; model.PhoneCell)
          &lt;/div&gt;
  
          &lt;div class="form-label"&gt;
              @Html.LabelFor(model =&gt; model.Email, "Email Address", new { style = "display:inline;" })
          &lt;/div&gt;
  
          &lt;div class="form.field"&gt;
              @Html.EditorFor(model =&gt; model.Email)
              @Html.ValidationMessageFor(model =&gt; model.Email)
          &lt;/div&gt;
  
          &lt;div class="form-label"&gt;
              @Html.LabelFor(model =&gt; model.Address, "Street Address", new { style = "display:inline;" })
          &lt;/div&gt;
  
          &lt;div class="form.field"&gt;
              @Html.EditorFor(model =&gt; model.Address)
              @Html.ValidationMessageFor(model =&gt; model.Address)
          &lt;/div&gt;
  
          &lt;div class="form-label"&gt;
              @Html.LabelFor(model =&gt; model.City, "City", new { style = "display:inline;" })
          &lt;/div&gt;
  
          &lt;div class="form.field"&gt;
              @Html.EditorFor(model =&gt; model.City)
              @Html.ValidationMessageFor(model =&gt; model.City)
          &lt;/div&gt;
  
          &lt;div class="form-label"&gt;
              @Html.LabelFor(model =&gt; model.State, "State", new { style = "display:inline;" })
          &lt;/div&gt;
  
          &lt;div class="form.field"&gt;
              @Html.EditorFor(model =&gt; model.State)
              @Html.ValidationMessageFor(model =&gt; model.State)
          &lt;/div&gt;
  
          &lt;div class="form-label"&gt;
              @Html.LabelFor(model =&gt; model.ZipCode, "Zip Code", new { style = "display:inline;" })
          &lt;/div&gt;
  
          &lt;div class="form.field"&gt;
              @Html.EditorFor(model =&gt; model.ZipCode)
              @Html.ValidationMessageFor(model =&gt; model.ZipCode)
  
          &lt;/div&gt;
  
      &lt;br /&gt;
      &lt;div&gt;
          &lt;input type="submit" class="btn-custom" value="Submit" /&gt;
          &lt;input type="reset" class="btn-custom" value="Clear" /&gt;
      &lt;/div&gt;
  
          &lt;/fieldset&gt;
      &lt;/form&gt;
  }
  </code></pre>