title: How to upload image to SQL with MVC3 within one Form using ADO.NET?
body: <p>I am having trouble putting image upload field within the rest of the fieldset, mainly becaouse i need two Actions for one Form, correct me if I am wrong! Aloso when using Model to render fields in View, there is no @Html.UploadField or similar.... Any ideas?</p>
  
  <p>My goal is to save image in the same row as the rest of the form...</p>
  
  <p><strong>MODEL:</strong></p>
  
  <pre><code> public class MyModel
   {
      [HiddenInput(DisplayValue = false)]
      public int ProductID { get; set; }
  
      [Display(Name = "Author: ")]
      public string Author { get; set; }
  
      [Display(Name = "Title: ")]
      public string Title { get; set; }
  
      public byte[] ImageData { get; set; }
  
      [HiddenInput(DisplayValue = false)]
      public string ImageMimeType { get; set; }
  
  }
  </code></pre>
  
  <p><strong>VIEW:</strong></p>
  
  <pre><code>@model namespace.MyModel
  
  @using (Html.BeginForm("AddForm", "Admin"))
  {
  
  &lt;div&gt;
      &lt;fieldset&gt;
          &lt;legend&gt;New Article&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(m =&gt; m.Author)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.TextBoxFor(m =&gt; m.Author)
              @Html.ValidationMessageFor(m =&gt; m.Author)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(m =&gt; m.Title)
          &lt;/div&gt; 
           &lt;div class="editor-field"&gt;
              @Html.TextBoxFor(m =&gt; m.Title)
          &lt;/div&gt;
  
           @using (Html.BeginForm("SaveImage", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
          {   
              &lt;div class="editor-label"&gt;Image&lt;/div&gt;
              &lt;div class="editor-field"&gt;
                  &lt;div&gt;Upload image: &lt;input type="file" name="Image" /&gt;&lt;/div&gt;
              &lt;/div&gt;
  
          }
  
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  &lt;/div&gt;
  }
  </code></pre>
  
  <p><strong>CONTROLLER</strong></p>
  
  <pre><code> //AddForm
      [HttpPost]
      public ActionResult AddForm(MyModel model)
      {
          if (ModelState.IsValid)
          {
  
              string connStr = ConfigurationManager.ConnectionStrings["AdminServices"].ConnectionString;
  
              using (SqlConnection connection = new SqlConnection(connStr))
              {
                  connection.Open();
  
                  using (SqlCommand command = new SqlCommand("insert into MyTable(DbAuthor, DbTitle) values(@author, @title)", connection))
                  {
                      command.Parameters.AddWithValue("@author", model.Author);
                      command.Parameters.AddWithValue("@title", model.Title);
  
                      command.ExecuteNonQuery();
  
                  }
              }
          }
  
          return RedirectToAction("Index", "Admin");
      }
  
      //SaveImage
      [Authorize]
      [HttpPost]
      public ActionResult SaveImage(MyModel zan, HttpPostedFileBase image)
      {
          if (ModelState.IsValid)
          {
              if (image != null)
              {
                  zan.ImageMimeType = image.ContentType;
                  zan.ImageData = new byte[image.ContentLength];
                  image.InputStream.Read(zan.ImageData, 0, image.ContentLength);
  
  
                  string connStr = ConfigurationManager.ConnectionStrings["AdminServices"].ConnectionString;
  
                  using (SqlConnection connection = new SqlConnection(connStr))
                  {
                      connection.Open();
  
                      using (SqlCommand command = new SqlCommand("insert into MyTable(DbImage) values(@image)", connection))
                      {
                          command.Parameters.AddWithValue("@image", zan.ImageData);
                          command.ExecuteNonQuery();
  
                      }
                  }
                  return RedirectToAction("Index", "Admin");
              }
  
          }
          return RedirectToAction("Index", "Admin");
      }
  </code></pre>
  
  <p>My problem is to put these two methods into one, and to have native MVC3 uploadfield in View... </p>