1-) C# MVC - Button Click ve bootstrap
nuget'den -> bootstrap yaz ekle
<a class="btn btn-primary" href="/departman/guncelle/@departman.id">Güncelle</a>
yukarıdaki kod departman controllerdeki güncelle actionresult'unu çalıştırır. @departman.id yerine sayı yazarak parametre gönderebilirsin
2-) HTML Helper ile Button click yapmak bunun kullan(C# MVC)
ilk parametresi : Captionu
ikinci parametresi : ActionResult
ucuncu parametresi : satırdaki id(foreach)
4 üncü parametresi : htmlAttributes
@Html.ActionLink("Sil","Sil",new { id=item.id},new { @class="btn btn-danger"})
açıklama : bulunduğun cshtml'in controlündeki Sil Actionresultunu tetikler.
public ActionResult Sil(int id)
{
var model = db.personel.Find(id);
if (model==null)
{
return HttpNotFound();
}
db.personel.Remove(model);
db.SaveChanges();
return RedirectToAction("Index");
}
public ActionResult Guncelle(int id)
{
var model = new PersonelFormViewModel()
{
Departmanlar=db.departman.ToList(),
Personel=db.personel.Find(id)
};
return View("PersonelForm",model);
}
public ActionResult Kaydet(personel personel)
{
if (personel.id==0)
{
db.personel.Add(personel);
}else// güncelleme
{
db.Entry(personel).State=System.Data.Entity.EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("Index");
}