This example shows how to add and update customers to the database with Entity Framework in a web application created using MVC (Model-View-Controller) architecture.
This example handles adding and updating customers in the same Controller using MVC 5 and Entity Framework. To manage database operations, you need to create the MyDbContext class and configure your database using the Entity Framework Code First approach. This example can help you understand basic CRUD (Create, Read, Update, Delete) operations.
using System.ComponentModel.DataAnnotations;
public class Musteri
{
public int MusteriId { get; set; }
[Required]
[StringLength(50)]
public string Ad { get; set; }
[Required]
[StringLength(50)]
public string Soyad { get; set; }
[StringLength(100)]
public string Adres { get; set; }
[StringLength(20)]
public string Telefon { get; set; }
[Required]
[StringLength(100)]
[EmailAddress]
public string Email { get; set; }
}
using System.Linq;
using System.Web.Mvc;
public class MusteriController : Controller
{
private MyDbContext dbContext = new MyDbContext(); // Veritabanı bağlamı
public ActionResult MusteriEkleGuncelle(int? musteriId)
{
Musteri musteri;
if (musteriId.HasValue)
{
// Müşteri güncelleme için sayfasını göster
musteri = dbContext.Musteriler.Find(musteriId);
}
else
{
// Yeni müşteri eklemek için boş bir müşteri oluştur
musteri = new Musteri();
}
return View("MusteriEkleGuncelle", musteri);
}
[HttpPost]
public ActionResult MusteriEkleGuncelle(Musteri musteri)
{
if (ModelState.IsValid)
{
if (musteri.MusteriId == 0)
{
// Yeni müşteri ekleniyor
dbContext.Musteriler.Add(musteri);
}
else
{
// Müşteri güncelleniyor
var existingMusteri = dbContext.Musteriler.Find(musteri.MusteriId);
if (existingMusteri != null)
{
existingMusteri.Ad = musteri.Ad;
existingMusteri.Soyad = musteri.Soyad;
existingMusteri.Adres = musteri.Adres;
existingMusteri.Telefon = musteri.Telefon;
existingMusteri.Email = musteri.Email;
}
}
dbContext.SaveChanges();
return RedirectToAction("MusteriListesi");
}
return View("MusteriEkleGuncelle", musteri);
}
public ActionResult MusteriListesi()
{
var musteriler = dbContext.Musteriler.ToList();
return View("MusteriListesi", musteriler);
}
}
@model YourNamespace.Musteri
@using (Html.BeginForm("MusteriEkleGuncelle", "Musteri", FormMethod.Post))
{
@Html.HiddenFor(model => model.MusteriId)
<div class="form-group">
@Html.LabelFor(model => model.Ad)
@Html.TextBoxFor(model => model.Ad, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Ad)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Soyad)
@Html.TextBoxFor(model => model.Soyad, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Soyad)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Adres)
@Html.TextBoxFor(model => model.Adres, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Telefon)
@Html.TextBoxFor(model => model.Telefon, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email)
@Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Email)
</div>
<button type="submit" class="btn btn-primary">Kaydet</button>
}