0

I am currently on the way of creating a web site using c# .net framework. I can add picture, name, explanation and price. When click on edit and change something the picture that i uploaded doesn't show up. I checked for the problem and realised that after my edit under RESIM column picture name doesn't show up. It writes NULL. I tried to change things in Controller but didn't work. Also when i delete something picture stay under wwwroot folder. My code is here:

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);
        }
    }
}
  • 1
    You posted [too much code](https://idownvotedbecau.se/toomuchcode/); probably because you [don't know what causes the problem](https://idownvotedbecau.se/unclearquestion). You [haven't provided debugging details](https://idownvotedbecau.se/noexceptiondetails/). All these things suggest you have [done no debugging](https://idownvotedbecau.se/nodebugging/). You need to do that before posting to Stack Overflow. – Dour High Arch Jul 24 '21 at 00:43
  • okay. thank you. when i debug no error returns. i will try to figure a way to run this. if any error occurs i will inform here. – About Channel Jul 24 '21 at 05:51
  • One tip from my side, you do not need to check if directory doesn't exist to create it. Directory.CreateDirectory will create directory only if it doesn't exist as per documentation https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.createdirectory?view=net-5.0 – EKOlog Jul 24 '21 at 09:46
  • Thank you @EKOlog it solved my problem. – About Channel Jul 25 '21 at 17:33

0 Answers0