27 Ocak 2019 Pazar

index

@model IEnumerable<Bgm.Models.EntityFramework.Marka>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<a href="/Marka/ekleGuncelle/0">ekle</a>
<table class="table tab-content">
    <thead>
    <tr>
        <th>adi</th>
        <th>düzenle</th>
        <th>sil</th>
    </tr>


    </thead>
    <tbody>
    @foreach (var i in Model)
    {
        <tr>
            <td>@i.Marka1</td>
            <td>
                <a href="/Marka/ekleGuncelle/@i.Id">düzenle</a>

            </td><td>
                <a href="/Marka/sil/@i.Id">sil</a>

            </td>



        </tr>
       
    }



    </tbody>



</table>

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");
            }
        }
    }
 
}