-2

Hello Everybody Good day My English is not very good Poo Do not Look Mvc 5 New Start My Blog Site. I got the error

  1. I List Categories, I Provide Entrance to Other Areas
  2. When I Select Photo When I Select Time

I uploaded the picture and I share the link. I am taking this mistake. Could you show me the way to this error? Thank you for your time

enter image description here

namespace MvcSite.Controllers
{
    public class AdminMakaleController : Controller
    {
        MvcblogDb db = new MvcblogDb();
        // GET: AdminMakale
        public ActionResult Index()
        {
            var makale = db.Makale.ToList();
            return View(makale);
        }

        // GET: AdminMakale/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: AdminMakale/Create
        public ActionResult Create()
        {
            ViewBag.KategoriId = new SelectList(db.Kategori, "KategoriId", "KategoriAdi");
            return View();
        }

        // POST: AdminMakale/Create
        [HttpPost]
        public ActionResult Create(Makale makale, string Etiket, HttpPostedFile Foto)
        {

            if (ModelState.IsValid)
            {
                if (Foto != null)
                {
                    WebImage img = new WebImage(Foto.InputStream);
                    FileInfo fotoinfo = new FileInfo(Foto.FileName);
                    string newfoto = Guid.NewGuid().ToString() + fotoinfo.Extension;
                    img.Resize(800, 350);
                    img.Save("~/Uploads/MakaleFoto/" + newfoto);
                    makale.Foto = "/Uploads/MakaleFoto/" + newfoto;
                }
                if (Etiket != null)
                {
                    string[] etiketdizi = Etiket.Split(',');
                    foreach (var i in etiketdizi)
                    {
                        var yenietiket = new Etiket { EtiketAdi = i };
                        db.Etiket.Add(yenietiket);
                        makale.Etiket.Add(yenietiket);
                    }
                }
                db.Makale.Add(makale);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        // GET: AdminMakale/Edit/5
        public ActionResult Edit(int id)
        {
            var makales = db.Makale.Where(m => m.MakaleId == id).SingleOrDefault();
            if (makales == null)
            {
                return HttpNotFound();

            }
            ViewBag.KategoriId = new SelectList(db.Kategori, "KategoriId", "KategoriAdi", makales.KategoriId);

            return View(makales);
        }

        // POST: AdminMakale/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, HttpPostedFile Foto, Makale makale)
        {
            try
            {
                var makales = db.Makale.Where(m => m.MakaleId == id).SingleOrDefault();

                if (Foto != null)
                {
                    if (System.IO.File.Exists(Server.MapPath(makales.Foto)))
                    {
                        System.IO.File.Delete(Server.MapPath(makales.Foto));
                    }
                    WebImage img = new WebImage(Foto.InputStream);
                    FileInfo fotoinfo = new FileInfo(Foto.FileName);
                    string newfoto = Guid.NewGuid().ToString() + fotoinfo.Extension;
                    img.Resize(800, 350);
                    img.Save("~/Uploads/MakaleFoto/" + newfoto);
                    makale.Foto = "/Uploads/MakaleFOTO/" + newfoto;
                    makales.Baslik = makale.Baslik;
                    makales.İcerik = makale.İcerik;
                    makales.KategoriId = makale.KategoriId;
                    db.SaveChanges();

                }
                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.KategoriId = new SelectList(db.Kategori, "KategoriId", "KategoriAdi", makale.KategoriId);
                return View(makale);
            }
        }

        // GET: AdminMakale/Delete/5
        public ActionResult Delete(int id)
        {
            var makale = db.Makale.Where(m => m.MakaleId == id).SingleOrDefault();
            if (makale == null)
            {
                return HttpNotFound();
            }
            return View(makale);
        }

        // POST: AdminMakale/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var makales = db.Makale.Where(m => m.MakaleId == id).SingleOrDefault();
                if (makales == null)
                {
                    return HttpNotFound();
                }
                if (System.IO.File.Exists(Server.MapPath(makales.Foto)))
                {
                    System.IO.File.Delete(Server.MapPath(makales.Foto));
                }
                foreach (var i in makales.Yorum.ToList())
                {
                    db.Yorum.Remove(i);
                }
                foreach (var i in makales.Etiket.ToList())
                {
                    db.Etiket.Remove(i);
                }
                db.Makale.Remove(makales);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {

                return View();
            }
        }
    }
}
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
  • https://stackoverflow.com/questions/25861635/mvc-best-way-to-populate-html-dropdownlist/25861739#25861739 – Tushar Gupta Jul 30 '17 at 14:03
  • It means the value of `KategoriId` is `null` (you do not set it in the POST method before you return the view as you did in the GET method) –  Jul 30 '17 at 22:40

1 Answers1

0

Try to use a DropDownListFor instead of a DropdownList. The error you mention means that you are having NULL in the SelectListItem. You should create a list of ListItem in the DropDownList.

(I'm not sure if I'm correct or not. I'm just trying to help quickly.)

robinCTS
  • 5,746
  • 14
  • 30
  • 37