编辑和显示实体图像

时间:2015-06-02 20:13:17

标签: c# asp.net-mvc entity-framework razor

我希望能够用新的替换旧图像。我在entites之间有一对多的关系 - >一个产品的许多图像。

如果我更改图像,那么旧版本将保留在数据库中。它们永远不会被使用。经过一段时间,数据库能够自己做大。如何删除这些旧文件?

public ActionResult Edit(int? id)
{
    if (id == null)
        { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); }

    Product product = db.Products.Include(p => p.FilePaths).Where(i => i.ProductId == id).Single();    
    if (product == null)
        { return HttpNotFound(); }

    return View(product);
}

[HttpPost]
public ActionResult Edit(int id, IEnumerable<HttpPostedFileBase> uploads)
{
    var product = db.Products.Include(p => p.FilePaths).Where(p => p.ProductId == id).Single();

    if (ModelState.IsValid)
    {
        try
        {
            product.FilePaths = new List<FilePath>();

            foreach (var upload in uploads)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    var photo = new FilePath
                    {
                        FileName = Path.GetFileName(upload.FileName),
                        FileType = FileType.Photo
                    };
                    product.FilePaths.Add(photo);
                    upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images"), photo.FileName));
                }
            }

            db.Entry(product).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        catch (RetryLimitExceededException)
        {
            ModelState.AddModelError("", "Unable to save changes. Contact with administrator.");
        }
    }
    return View(product);
}

我对Edit()的看法是:

@model Domain.Entities.Product
@using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {

// code ommited for brevity..
<div class="row">
    @Html.Label("Images:", new { @class = "control-label col-md-2" })
    <div class="col-md-1">
        <input type="file" name="uploads" id="upload1" />
    </div>
    <div class="col-md-offset-2 col-md-1">
        <input type="file" name="uploads" id="upload2" />
    </div>
    <div class="col-md-offset-2 col-md-1">
        <input type="file" name="uploads" id="upload3" />
    </div>
</div>
//code ommited for brevity..

如果有人能告诉我如何正确地改变这一点,我将不胜感激。

我获得的产品的第一张图像是产品获得的图像的数量。我想要显示所有图像一次。 我在使用图像目录时遇到了麻烦。

<div class="row">
    <label class="control-label col-md-2"> Obecne zdjęcia:</label>
    @foreach (var item in Model.FilePaths)
    {
        <div class="col-md-offset-1 col-md-1">
            <img src="~/Content/Images/@Model.FilePaths.FirstOrDefault(f => f.FileType == FileType.Photo).FileName" alt="" style="width:auto; height:100px">
        </div>
    }
</div>

我的FilePath模型类:

public class FilePath
{
    public int FilePathId { get; set; }

    public string FileName { get; set; }

    public FileType FileType { get; set; }

    public int? ProductId { get; set; }

    //navigation..
    public virtual Product Product { get; set; }
}

每一个帮助都非常感激。谢谢!

0 个答案:

没有答案