Saya pemula. Saat memperbarui objek entitas, yaitu gambar, gambar lama tidak dihapus, meskipun semuanya ditulis dalam metode. Saya tidak mengerti mengapa ImageUrl adalah nol. Ketika saya mendapatkan objek ImageUrl ada di sana, dan ketika saya mencoba memperbarui ImageUrl kosong.

Bagian belakang:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Upsert(Department item)
    {
        if (ModelState.IsValid)
        {
            string webRootPath = _webHost.WebRootPath;
            var files = HttpContext.Request.Form.Files;

            if (files.Count > 0)
            {
                string fileName = Guid.NewGuid().ToString();
                var uploads = Path.Combine(webRootPath, @"images\department");
                var extenstion = Path.GetExtension(files[0].FileName);

                if (item.ImageUrl != null) // ImageUrl is null (0_0)
                {
                    // Update data with image
                    var imagePath = Path.Combine(webRootPath, item.ImageUrl.TrimStart('\\'));

                    if (System.IO.File.Exists(imagePath))
                    {
                        System.IO.File.Delete(imagePath);
                    }
                }

                using (var fileStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create))
                {
                    await files[0].CopyToAsync(fileStreams);
                }

                item.ImageUrl = @"\images\department\" + fileName + extenstion;
            }
            else
            {
                // Update data without update image
                if (item.Id != 0)
                {
                    var model = await _db.Departments.FindAsync(item.Id);
                    item.ImageUrl = model.ImageUrl;
                }
            }

            if (item.Id == 0)
            {
                await _db.Departments.AddAsync(item);
            }
            else
            {
                _db.Departments.Update(item);
            }

            await _db.SaveChangesAsync();

            return RedirectToAction(nameof(Index));
        }
        else
        {
            if (item.Id != 0)
            {
                item = await _db.Departments.FindAsync(item.Id);
            }
        }

        return View(item);
    }

Paling depan

@model Department
        
 @{
     var title = "Add";
  }
    
    <div class="container">
        <form method="post" enctype="multipart/form-data">
            <div class="row p-2 border" style="margin-top:10%;">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    
                @if (Model.Id != 0)
                {
                    // Edit
                    title = "Edit";
                    <input type="hidden" asp-for="Id" />
                }
    
                <div class="col-12 border-bottom">
                    <h3>@title</h3>
                </div>
    
                <div class="col-12 pt-4">
    
                    @if (Model.Id != 0)
                    {
                        <div class="form-group row p-2">
                            <img src="@Model.ImageUrl" />
                        </div>
                    }
    
                    <div class="form-group row">
                        <div class="col-4">
                            <label asp-for="Name"></label>
                        </div>
                        <div class="col-8">
                            <input asp-for="Name" class="form-control" />
                            <span asp-validation-for="Name" class="text-danger"></span>
                        </div>
                    </div>
    
                    <div class="form-group row">
                        <div class="col-4">
                            Image
                        </div>
                        <div class="col-8">
                            <input type="file" name="files" id="uploadBox" multiple class="form-control" />
                        </div>
                    </div>
    
                    <div class="form-group row">
                        <div class="col-4">
                            <label asp-for="Address"></label>
                        </div>
                        <div class="col-8">
                            <input asp-for="Address" class="form-control" />
                            <span asp-validation-for="Address" class="text-danger"></span>
                        </div>
                    </div>
    
                    <div class="form-group row">
                        <div class="col-4">
                            <label asp-for="Phone"></label>
                        </div>
                        <div class="col-8">
                            <input asp-for="Phone" class="form-control" />
                            <span asp-validation-for="Phone" class="text-danger"></span>
                        </div>
                    </div>
    
                    <div class="form-group row">
                        <div class="col-4">
                            <label asp-for="Description"></label>
                        </div>
                        <div class="col-8">
                            <textarea asp-for="Description" class="form-control" rows="15"></textarea>
                        </div>
                    </div>
    
                    <div class="form-group row">
                        <div class="col-8 offset-4">
                            @if (Model.Id != 0)
                            {
                                <partial name="_EditAndBackToListButton" model="Model.Id" />
                            }
                            else
                            {
                                <div class="row">
                                    <div class="col">
                                        <button type="submit" onclick="return validateInput()" class="btn btn-sm btn-outline-success form-control">Add</button>
                                    </div>
                                    <div class="col">
                                        <a asp-action="Index" class="btn btn-sm btn-outline-primary form-control text-center">Back</a>
                                    </div>
                                </div>
                            }
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>

GitHub saya https://github.com/ValencyJacob/DepartmentManagment/ blob/master/DepartmentManagment/Controllers/DepartmentController.cs

1
Kazadun 26 Mei 2021, 09:57

1 menjawab

Jawaban Terbaik

Jika maksud Anda item.ImageUrl(ImgUrl lama) di Upsert(Department item) saat Anda memperbarui objek, Anda perlu menambahkan input tersembunyi untuk mengikat ImageUrl dalam tampilan. Berikut adalah demo-:

Melihat:

...

    @if (Model.Id != 0)
                    {
                        <div class="form-group row p-2">
                            <img src="@Model.ImageUrl" />
                            <input hidden asp-for="ImageUrl" />
                        </div>
                    }
...

Hasil: masukkan deskripsi gambar di sini

Yiyi You 27 Mei 2021, 02:25