title: MVC4 C# - Want to save a image to a folder and saving the url in database
body: <p>I'm pretty new with MVC4 coding. Have been programming in SharePoint before. </p>
  
  <p>The issue i have is that i want to save a image to a specific folder (Let say App_Data) and also save the url of the image to a string in a database. 
  Would be great if anyone could help me with this issue. </p>
  
  <p>The code i got right now is.</p>
  
  <p>Models > ImageUpload.cs</p>
  
  <pre><code>public class ImageUpload
  
  {
      public int ID { get; set; }
      public string Title { get; set; } 
      public string Url { get; set; }
  }
  
  public class ImageUploadDBContext : DbContext
  {
      public DbSet&lt;ImageUpload&gt; ImageUploads { get; set; }
  }
  </code></pre>
  
  <p>Controllers > ImageUploadController.cs</p>
  
  <pre><code>[HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create(ImageUpload imageupload)
      {
          if (ModelState.IsValid)
          {
              db.ImageUploads.Add(imageupload);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          return View(imageupload);
      }
  
  [HttpPost]
      public ActionResult Upload(HttpPostedFileBase[] files)
      {
          foreach (HttpPostedFileBase file in files)
          {
              string picture = Path.GetFileName(file.FileName);
              string path = Path.Combine(Server.MapPath("~/App_Data"), picture);
              string[] paths = path.Split('.');
              string time = DateTime.UtcNow.ToString();
              time = time.Replace(" ", "-");
              time = time.Replace(":", "-"); 
              file.SaveAs(paths[0] + "-" + time + ".jpg");
          }
          ViewBag.Message = "File(s) uploaded successfully";
          return RedirectToAction("Index");
      }
  </code></pre>
  
  <p>View > ImageUpload > Index.cshtml</p>
  
  <pre><code>@{
  ViewBag.Title = "Index";
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Title)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Url)
          &lt;/th&gt;
          &lt;th&gt;
              Preview
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
      @foreach (var item in Model) {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Title)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Url)
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;img border="0" src="@Html.DisplayFor(modelItem =&gt; item.Url)" alt="@Html.DisplayFor(modelItem =&gt; item.Title)"&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                  @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                  @Html.ActionLink("Delete", "Delete", new { id=item.ID })
              &lt;/td&gt;
          &lt;/tr&gt;    
      }
  &lt;/table&gt;
  </code></pre>
  
  <p>View > ImageUpload > Create.cshtml</p>
  
  <pre><code>@{
      ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  @using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;ImageUpload&lt;/legend&gt;
  
              @using (Html.BeginForm())
              {
                      &lt;div class="editor-label"&gt;
                          &lt;b&gt;@Html.LabelFor(Model =&gt; Model.Title)&lt;/b&gt;
                      &lt;/div&gt;
                      &lt;div class="editor-field"&gt;
                          @Html.EditorFor(Model =&gt; Model.Title)
                          @Html.ValidationMessageFor(Model =&gt; Model.Title)
                      &lt;/div&gt;
                      &lt;div class="editor-label"&gt;
                          &lt;b&gt;@Html.LabelFor(Model =&gt; Model.Url)&lt;/b&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          @Html.EditorFor(Model =&gt; Model.Url)
                          &lt;input type="file" name="files" value="" multiple="multiple"/&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;input type="submit" value="Submit" /&gt;
                      &lt;/div&gt;
              }
  
  
      &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>