shm*_*shm 3 asp.net asp.net-mvc if-statement updates asp.net-mvc-5
Asp.net MVC 5我在with更新数据时遇到问题Razor Engine。我的更新代码工作正常,但我有一些问题。当我更新时Image,旧图像保留在其中Images folder。如果旧图像发生变化,我想删除它。如果不改变的话我想保持旧的形象。我该怎么做 ?非常感谢您的帮助,我不知道如何为此编写 if 语句:/
CarouselRepositories.cs
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;
        }
    }
管理控制器
[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');");
        }
    }
编辑Carousel.cshtml:
@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()
<div class="form-horizontal">
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.CarouselID)
    <div class="form-group">
        @Html.LabelFor(model => model.CarouselSubject, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CarouselSubject)
            @Html.ValidationMessageFor(model => model.CarouselSubject)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.CarouselInfo, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CarouselInfo)
            @Html.ValidationMessageFor(model => model.CarouselInfo)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.CarouselImage, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @*@Html.EditorFor(model => model.CarouselImage)*@
            @Html.ImageFor(model => model.CarouselImage, new {width="300"},"","Images","Carousel")
            @Html.Upload("UploadImage")
            @Html.HiddenFor(model => model.CarouselImage)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}
更新的 Amin 控制器:
 [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');");
        }
    }
小智 5
假设你想删除当前文件如果 的值UploadImage不在nullPOST 方法中,那么你可以使用该System.IO.File.Delete方法
private const string _ImagesPath = "~/Images/Carousel";
[HttpPost]
public ActionResult EditCarousel(Carousel carousel, HttpPostedFileBase UploadImage)
{
  if (ModelState.IsValid)
  {
    CarouselRepositories blCarousel = new CarouselRepositories();
    if (UploadImage != null)
    {
      // Delete exiting file
      System.IO.File.Delete(Path.Combine(Server.MapPath(_ImagesPath), carousel.CarouselImage));
      // Save new file
      string fileName = Guid.NewGuid() + Path.GetFileName(UploadImage.FileName);
      string path = Path.Combine(Server.MapPath(_ImagesPath), fileName);
      UploadImage.SaveAs(path)
      carousel.CarouselImage = fileName;
    }
    if (blCarousel.Update(carousel))
    {
      ....
    }
    else
    {
      ....
    }
  }
  else
  {
    ....
  }
}
| 归档时间: | 
 | 
| 查看次数: | 17478 次 | 
| 最近记录: |