title: How update/edit uploaded files in asp.net mvc 5 without redundancy?
body: <p>I have a problem with update data in <code>Asp.net MVC 5</code> with <code>Razor Engine</code> . 
  my Update code works fine but I have some problems with it . When I update <code>Image</code> , old image stays in <code>Images folder</code>.  I want to delete old image if it changed . and I want to stay old image if it didn't change . 
  How can I do it ? 
  Thanks a lot for any help 
  I don't know how write if statement for this :/ </p>
  
  <p><strong>CarouselRepositories.cs</strong></p>
  
  <pre><code>public bool Update(NP1.Models.Carousel entity, bool autoSave = true)
      {
          try
          {
  
              db.Carousels.Attach(entity);
              db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
              if (autoSave)
                  return Convert.ToBoolean(db.SaveChanges());
              else
                  return false;
          }
          catch
          {
              return false;
          }
      }
  </code></pre>
  
  <p><strong>Admin controller</strong></p>
  
  <pre><code>[HttpGet]
      public ActionResult EditCarousel(int id)
      {
  
          var load = db.Carousels.Find(id);
          return View(load);
      }
  
      [HttpPost]
      public ActionResult EditCarousel(Carousel carousel, HttpPostedFileBase UploadImage)
      {
          CarouselRepositories blCarousel = new CarouselRepositories();
          string path = "";
          var fileName = "";
          var rondom = "";
          if (UploadImage != null)
          {
  
              fileName = Path.GetFileName(UploadImage.FileName);
              rondom = Guid.NewGuid() + fileName;
              path = System.IO.Path.Combine(
                                    Server.MapPath("~/Images/Carousel"), rondom);
  
              carousel.CarouselImage = rondom;
          }
          if (ModelState.IsValid)
          {
  
              UploadImage.SaveAs(path);
              carousel.CarouselImage = rondom;
              if (blCarousel.Update(carousel))
              {
                  return JavaScript("alert('Carousel slide added');");
              }
              else
              {
                  return JavaScript("alert('didn't add');");
              }
          }
          else
          {
              return JavaScript("alert('Error');");
          }
  
      }
  </code></pre>
  
  <p><strong>EditCarousel.cshtml:</strong></p>
  
  <pre><code>@model NP1.Models.Carousel
  
  @{
  ViewBag.Title = "EditCarousel";
  Layout = "~/Views/Admin/AdminLayout.cshtml";
  }
   @using (Html.BeginForm("EditCarousel", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", id = "myUploadForm" }))
  {
  @Html.AntiForgeryToken()
  
  &lt;div class="form-horizontal"&gt;
  
      @Html.ValidationSummary(true)
      @Html.HiddenFor(model =&gt; model.CarouselID)
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.CarouselSubject, new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt; model.CarouselSubject)
              @Html.ValidationMessageFor(model =&gt; model.CarouselSubject)
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.CarouselInfo, new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt; model.CarouselInfo)
              @Html.ValidationMessageFor(model =&gt; model.CarouselInfo)
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.CarouselImage, new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @*@Html.EditorFor(model =&gt; model.CarouselImage)*@
              @Html.ImageFor(model =&gt; model.CarouselImage, new {width="300"},"","Images","Carousel")
              @Html.Upload("UploadImage")
              @Html.HiddenFor(model =&gt; model.CarouselImage)
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-offset-2 col-md-10"&gt;
              &lt;input type="submit" value="Save" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  }
  </code></pre>
  
  <p><strong>Updated Amin Controller :</strong></p>
  
  <pre><code> [HttpPost]
      public ActionResult EditCarousel(Carousel carousel, HttpPostedFileBase UploadImage)
      {
          CarouselRepositories blCarousel = new CarouselRepositories();
          string path = "";
          var fileName = "";
          var rondom = "";
          if (UploadImage != null)
          {
  
              fileName = Path.GetFileName(UploadImage.FileName);
              rondom = Guid.NewGuid() + fileName;
              path = System.IO.Path.Combine(
                                    Server.MapPath("~/Images/Carousel"), rondom);
  
              carousel.CarouselImage = rondom;
          }
          else
          {
              fileName = carousel.CarouselImage;
              path = System.IO.Path.Combine(
                                    Server.MapPath("~/Images/Carousel"), fileName);
          }
          if (ModelState.IsValid)
          {
  
              UploadImage.SaveAs(path); // I got error in this line 
              carousel.CarouselImage = rondom;
              if (blCarousel.Update(carousel))
              {
                  return JavaScript("alert('Carousel slide added');");
              }
              else
              {
                  return JavaScript("alert('didn't add');");
              }
          }
          else
          {
              return JavaScript("alert('Error');");
          }
  
      }
  </code></pre>