首页 > 解决方案 > 编辑后图片不显示。MySQL数据库图片目录为空

问题描述

我目前正在使用 c# .net 框架创建一个网站。我可以添加图片、名称、说明和价格。单击编辑并更改某些内容时,我上传的图片不显示。我检查了问题并意识到在我在 RESIM 列图片名称下进行编辑后没有显示。它写入 NULL。我试图改变 Controller 中的东西,但没有奏效。另外,当我删除某些图片时,请留在 wwwroot 文件夹下。我的代码在这里:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using ETicaret.Data;
using Eticaret.Models;
using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace Eticaret.Controllers
{
    public class UrunYonetimi : Controller
    {
        private readonly ETicaretContext _context;
        private readonly IWebHostEnvironment _hostEnvironment;

        public UrunYonetimi(ETicaretContext context, IWebHostEnvironment hostEnvironment)
        {
            _context = context;
            _hostEnvironment = hostEnvironment;
        }

        // GET: UrunYonetimi
        public async Task<IActionResult> Index()
        {
            return View(await _context.Urunler.ToListAsync());
        }

        // GET: UrunYonetimi/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var urun = await _context.Urunler
                .SingleOrDefaultAsync(m => m.id == id);
            if (urun == null)
            {
                return NotFound();
            }

            return View(urun);
        }

        // GET: UrunYonetimi/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: UrunYonetimi/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("id,Ad,Aciklama,Fiyat,Dosya")] Urun urun)
        {
            if (ModelState.IsValid)
            {
                var dosyaYolu =Path.Combine(
                    _hostEnvironment.WebRootPath,
                    "imajlar");
                if (Directory.Exists(dosyaYolu))
                {
                }
                else
                {
                    Directory.CreateDirectory(dosyaYolu);
                }

                var tamDosyaAdi = Path.Combine(dosyaYolu, urun.Dosya.FileName);

                using (var dosyaAkisi = new FileStream(tamDosyaAdi, FileMode.CreateNew))
                {
                    await urun.Dosya.CopyToAsync(dosyaAkisi);
                }

                urun.Resim = urun.Dosya.FileName;

                _context.Add(urun);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(urun);
        }

        // GET: UrunYonetimi/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var urun = await _context.Urunler.FindAsync(id);
            if (urun == null)
            {
                return NotFound();
            }
            return View(urun);
        }

        // POST: UrunYonetimi/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("id,Ad,Aciklama,Fiyat,Dosya")] Urun urun)
        {
            if (id != urun.id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(urun);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UrunExists(urun.id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(urun);
        }

        // GET: UrunYonetimi/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var urun = await _context.Urunler
                .FirstOrDefaultAsync(m => m.id == id);

           

            if (urun == null)
            {
                return NotFound();
            }

            return View(urun);
        }

        // POST: UrunYonetimi/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var urun = await _context.Urunler.FindAsync(id);
            _context.Urunler.Remove(urun);



            
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool UrunExists(int id)
        {
            return _context.Urunler.Any(e => e.id == id);
        }
    }
}

标签: c#.netasp.net-mvcomnisharp

解决方案


推荐阅读