27 Ocak 2019 Pazar

asp.net Entityframework crud işlemleri

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Bgm.Controllers
{
    using Bgm.Models.EntityFramework;

    public class MarkaController : Controller
    {
        TelefoncuEntities db = new TelefoncuEntities();
        // GET: Marka
        public ActionResult Index()
        {
     
            var model = db.Marka.ToList();
            return View(model);
        }
        [HttpGet]
        public ActionResult ekleGuncelle(int id)
        {
            if (id == 0)
            {
                return View(new Marka());
            }
            else
            {
                var model = this.db.Marka.Find(id);
                return this.View(model);
            }
        }
        [HttpPost]
        public ActionResult ekleGuncelle(Marka gelenMarka)
        {
            if (gelenMarka.Id == 0)
            {
                this.db.Marka.Add(gelenMarka);
                this.db.SaveChanges();


                Model mdl = new Model();
                mdl.MarkaId = db.Marka.Max(x=>x.Id);
                this.db.Model.Add(mdl);
                this.db.SaveChanges();

                return RedirectToAction("Index", "Marka");
            }
            else
            {
               Marka mrk=new Marka();
                mrk = this.db.Marka.Find(gelenMarka.Id);
                mrk.Marka1 = gelenMarka.Marka1;
                this.db.SaveChanges();
                return RedirectToAction("Index", "Marka");


            }
        }

        public ActionResult Sil(int id)
        {
            if (id != 0)
            {
                var model = this.db.Marka.Find(id);
                this.db.Marka.Remove(model);
                this.db.SaveChanges();
                return RedirectToAction("Index", "Marka");
            }
            else
            {
                return RedirectToAction("Index", "Marka");
            }
        }
    }
 
}

Hiç yorum yok:

Yorum Gönder