SampleController.cs
Dosyayı İndir
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication4.Controllers
{
public class SampleController : Controller
{
private MvcEntityDBEntities db = new MvcEntityDBEntities();
//
// GET: /Default1/
public ActionResult Index()
{
return View(db.Samples.ToList());
}
//
// GET: /Default1/Details/5
public ActionResult Details(long id = 0)
{
Sample sample = db.Samples.Single(s => s.SampleId == id);
if (sample == null)
{
return HttpNotFound();
}
return View(sample);
}
//
// GET: /Default1/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Default1/Create
[HttpPost]
public ActionResult Create(Sample sample)
{
if (sample.SampleName.StartsWith("X")) {
ModelState.AddModelError("","X İle Başlamaz!");
}
if (ModelState.IsValid)
{
db.Samples.AddObject(sample);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(sample);
}
//
// GET: /Default1/Edit/5
public ActionResult Edit(long id = 0)
{
Sample sample = db.Samples.Single(s => s.SampleId == id);
if (sample == null)
{
return HttpNotFound();
}
return View(sample);
}
//
// POST: /Default1/Edit/5
[HttpPost]
public ActionResult Edit(Sample sample)
{
if (ModelState.IsValid)
{
db.Samples.Attach(sample);
db.ObjectStateManager.ChangeObjectState(sample, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(sample);
}
//
// GET: /Default1/Delete/5
public ActionResult Delete(long id = 0)
{
Sample sample = db.Samples.Single(s => s.SampleId == id);
if (sample == null)
{
return HttpNotFound();
}
return View(sample);
}
//
// POST: /Default1/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(long id)
{
Sample sample = db.Samples.Single(s => s.SampleId == id);
db.Samples.DeleteObject(sample);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
Dosyayı İndir