Scaffolding Enhancements
Scaffolding is a feature through which we can generate code automatically using some predefined templates. Asp.Net MVC has built in scaffolding support and feature has been enhanced in MVC 5 by introduction of New Scaffold Menu item.
Let’s try to generate a controller for an existing Model class
public class PersonModel
{
[Key]
public int PersonId { get; set; }
public string PersonName { get; set; }
public int PersonAge { get; set; }
}
Right Click on project/controller -> Add -> New Scaffolded Item
Select MVC 5 Controller with views using Entity Framework and Click Add
Select Model and Data Context Classes and click Add
The generated Controller class
public class PersonModelsController : Controller
{
private PersonDataContext db = new PersonDataContext();
// GET: PersonModels
public ActionResult Index()
{
return View(db.PersonModels.ToList());
}
// GET: PersonModels/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
PersonModel personModel = db.PersonModels.Find(id);
if (personModel == null)
{
return HttpNotFound();
}
return View(personModel);
}
// GET: PersonModels/Create
public ActionResult Create()
{
return View();
}
// POST: PersonModels/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PersonId,PersonName,PersonAge")] PersonModel personModel)
{
if (ModelState.IsValid)
{
db.PersonModels.Add(personModel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(personModel);
}
// GET: PersonModels/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
PersonModel personModel = db.PersonModels.Find(id);
if (personModel == null)
{
return HttpNotFound();
}
return View(personModel);
}
// POST: PersonModels/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PersonId,PersonName,PersonAge")] PersonModel personModel)
{
if (ModelState.IsValid)
{
db.Entry(personModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(personModel);
}
// GET: PersonModels/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
PersonModel personModel = db.PersonModels.Find(id);
if (personModel == null)
{
return HttpNotFound();
}
return View(personModel);
}
// POST: PersonModels/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
PersonModel personModel = db.PersonModels.Find(id);
db.PersonModels.Remove(personModel);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}