Skip to content

Instantly share code, notes, and snippets.

@Jimshii
Created July 9, 2012 20:58
Show Gist options
  • Save Jimshii/3078856 to your computer and use it in GitHub Desktop.
Save Jimshii/3078856 to your computer and use it in GitHub Desktop.
PhonesController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using MvcApplication1.Services;
namespace MvcApplication1.Controllers
{
public class PhonesController : Controller
{
public ActionResult SearchIndex(string searchString)
{
var pnumbers = from m in db.Phones
select m;
if (!String.IsNullOrEmpty(searchString))
{
pnumbers = pnumbers.Where(s => s.Name.Contains(searchString));
}
return View(pnumbers);
}
private PhoneService _service = new PhoneService();
private PhoneDBContext db = new PhoneDBContext();
//
// GET: /Phones/
public ViewResult Index()
{
return View(db.Phones.ToList());
}
//
// GET: /Phones/Details/5
public ViewResult Details(int id)
{
Phone phone = db.Phones.Find(id);
return View(phone);
}
//
// GET: /Phones/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Phones/Create
[HttpPost]
public ActionResult Create(Phone phone)
{
if (ModelState.IsValid)
{
try
{
_service.AddPhone(phone);
return RedirectToAction("Index");
}
catch (Exception)
{
}
}
return View(phone);
}
//
// GET: /Phones/Edit/5
public ActionResult Edit(int id)
{
Phone phone = db.Phones.Find(id);
return View(phone);
}
//
// POST: /Phones/Edit/5
[HttpPost]
public ActionResult Edit(Phone phone)
{
if (ModelState.IsValid)
{
db.Entry(phone).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(phone);
}
//
// GET: /Phones/Delete/5
public ActionResult Delete(int id)
{
Phone phone = db.Phones.Find(id);
return View(phone);
}
//
// POST: /Phones/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Phone phone = db.Phones.Find(id);
db.Phones.Remove(phone);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment