微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

编辑后图片不显示 MySQL 数据库图片目录为空

如何解决编辑后图片不显示 MySQL 数据库图片目录为空

我目前正在使用 c# .net 框架创建网站。我可以添加图片名称、说明和价格。单击编辑并更改某些内容时,我上传图片没有显示。我检查了这个问题,并意识到在我在 RESIM 列图片名称下编辑后没有显示出来。它写NULL。我试图改变控制器中的东西,但没有奏效。此外,当我删除某些图片时,请保留在 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,see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id,[Bind("id,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);
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。