Last active
September 3, 2018 08:01
-
-
Save racsonp/cb7bd6f89cb7f3211ca20ae5bbaf6076 to your computer and use it in GitHub Desktop.
MVC.NET CRUD Controler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Install-Package Microsoft.AspNet.Mvc -Version 5.2.3 | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using WebApplication1.Models; | |
using System.Data.Entity; | |
namespace WebApplication1.Controllers | |
{ | |
public class PersonaController : Controller | |
{ | |
//El archova de abajo den Dbpruebamodel.edmx en tt --- Contex.CS | |
TestComp01Entities1 db = new TestComp01Entities1(); | |
// GET: Persona | |
public ActionResult Index() | |
{ | |
return View(db.Personas.ToList<Persona>()); | |
} | |
// GET: Persona/Details/5 | |
public ActionResult Details(int id) | |
{ | |
var query = ( from A in db.Personas where A.Id == id | |
select A).Single(); | |
return View((Persona)query); | |
} | |
// GET: Persona/Create | |
public ActionResult Create() | |
{ | |
return View(); | |
} | |
// POST: Persona/Create | |
[HttpPost] | |
public ActionResult Create([Bind(Exclude = "Id")] Persona objPersona) | |
{ | |
//try | |
//{ | |
if (!ModelState.IsValid) | |
{ | |
return View(); | |
} | |
db.Entry(objPersona).State = EntityState.Added; | |
db.SaveChanges(); | |
return Redirect("Index"); | |
//} | |
//catch | |
//{ | |
// return View(); | |
//} | |
} | |
// GET: Persona/Edit/5 | |
public ActionResult Edit(int id) | |
{ | |
var query = (from A in db.Personas | |
where A.Id == id | |
select A).Single(); | |
return View((Persona)query); | |
} | |
// POST: Persona/Edit/5 | |
[HttpPost] | |
public ActionResult Edit(int id, Persona persona) | |
{ | |
try | |
{ | |
if(!ModelState.IsValid) | |
{ | |
return View(); | |
} | |
db.Entry(persona).State = EntityState.Modified; | |
db.SaveChanges(); | |
// TODO: Add update logic here | |
return RedirectToAction("Index"); | |
} | |
catch | |
{ | |
return View(); | |
} | |
} | |
// GET: Persona/Delete/5 | |
public ActionResult Delete(int id) | |
{ | |
var query = (from A in db.Personas | |
where A.Id == id | |
select A).Single(); | |
return View((Persona)query); | |
} | |
// POST: Persona/Delete/5 | |
[HttpPost] | |
public ActionResult Delete(int id, Persona person ) | |
{ | |
try | |
{ | |
var query = (from A in db.Personas | |
where A.Id == id | |
select A).Single(); | |
person = (Persona)query; | |
db.Entry(person).State = EntityState.Deleted; | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
catch | |
{ | |
return View(); | |
} | |
} | |
} | |
} | |
--------------------------------------------------------------- | |
-------------------------------------------------------------- | |
----------------------------------------------------------------------------------------- | |
--------------------------------------------------------------- | |
---------------------------- | |
public class HijoesController : Controller | |
{ | |
private v15_JockeyTestComp01Entities db = new v15_JockeyTestComp01Entities(); | |
// GET: Hijoes | |
public ActionResult Index() | |
{ | |
var hijoes = db.Hijoes.Include(h => h.Persona); | |
return View(hijoes.ToList()); | |
} | |
// GET: Hijoes/Details/5 | |
public ActionResult Details(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Hijo hijo = db.Hijoes.Find(id); | |
if (hijo == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(hijo); | |
} | |
// GET: Hijoes/Create | |
public ActionResult Create() | |
{ | |
ViewBag.IdPersona = new SelectList(db.Personas, "Id", "Nombre"); | |
return View(); | |
} | |
// POST: Hijoes/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 = "IdHijo,IdPersona,Nombre,edad")] Hijo hijo) | |
{ | |
if (ModelState.IsValid) | |
{ | |
db.Hijoes.Add(hijo); | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
ViewBag.IdPersona = new SelectList(db.Personas, "Id", "Nombre", hijo.IdPersona); | |
return View(hijo); | |
} | |
// GET: Hijoes/Edit/5 | |
public ActionResult Edit(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Hijo hijo = db.Hijoes.Find(id); | |
if (hijo == null) | |
{ | |
return HttpNotFound(); | |
} | |
ViewBag.IdPersona = new SelectList(db.Personas, "Id", "Nombre", hijo.IdPersona); | |
return View(hijo); | |
} | |
// POST: Hijoes/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 = "IdHijo,IdPersona,Nombre,edad")] Hijo hijo) | |
{ | |
if (ModelState.IsValid) | |
{ | |
db.Entry(hijo).State = EntityState.Modified; | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
ViewBag.IdPersona = new SelectList(db.Personas, "Id", "Nombre", hijo.IdPersona); | |
return View(hijo); | |
} | |
// GET: Hijoes/Delete/5 | |
public ActionResult Delete(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Hijo hijo = db.Hijoes.Find(id); | |
if (hijo == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(hijo); | |
} | |
// POST: Hijoes/Delete/5 | |
[HttpPost, ActionName("Delete")] | |
[ValidateAntiForgeryToken] | |
public ActionResult DeleteConfirmed(int id) | |
{ | |
Hijo hijo = db.Hijoes.Find(id); | |
db.Hijoes.Remove(hijo); | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
db.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment