http://www.binaryintellect.net/articles/4a00a9ce-73e5-4d89-aaae-2d835eca0854.aspx
Models:
public class Customer
{
[EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public string CustomerContact { get; set; }
}
public class CustomersViewModel
{
public List Customers { get; set; }
public Customer SelectedCustomer { get; set; }
public string DisplayMode { get; set; }
}
Controllers:
namespace DisplayAllInOnePage.Controllers
{
public class CustomersController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Customers
public ActionResult Index()
{
//List customerList = new List();
//Customer customer1 = new Customer();
//customer1.CustomerID = 1;
//customer1.CustomerName = "Name1";
//customer1.CustomerContact = "Contact1";
//Customer customer2 = new Customer();
//customer2.CustomerID = 2;
//customer2.CustomerName = "Name2";
//customer2.CustomerContact = "Contact2";
//var all = from c in db.Customers select c;
//db.Customers.RemoveRange(all);
//db.SaveChanges();
//db.Customers.Add(customer1);
//db.Customers.Add(customer2);
//db.SaveChanges();
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = null;
return View(model);
//return View(db.Customers.ToList());
}
[HttpPost]
public ActionResult New()
{
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = null;
model.DisplayMode = "WriteOnly";
return View("Index", model);
}
[HttpPost]
public ActionResult Insert(Customer obj)
{
db.Customers.Add(obj);
db.SaveChanges();
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = db.Customers.Find(obj.CustomerID);
model.DisplayMode = "ReadOnly";
return View("Index", model);
}
[HttpPost]
public ActionResult Select(int id)
{
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = db.Customers.Find(id);
model.DisplayMode = "ReadOnly";
return View("Index", model);
}
[HttpPost]
public ActionResult Edit(int id)
{
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = db.Customers.Find(id);
model.DisplayMode = "ReadWrite";
return View("Index", model);
}
[HttpPost]
public ActionResult Update(Customer obj)
{
Customer existing = db.Customers.Find(obj.CustomerID);
existing.CustomerName = obj.CustomerName;
existing.CustomerContact = obj.CustomerContact;
db.SaveChanges();
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = existing;
model.DisplayMode = "ReadOnly";
return View("Index", model);
}
[HttpPost]
public ActionResult Delete(int id)
{
Customer existing = db.Customers.Find(id);
db.Customers.Remove(existing);
db.SaveChanges();
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = null;
model.DisplayMode = "";
return View("Index", model);
}
[HttpPost]
public ActionResult Cancel(int id)
{
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = db.Customers.Find(id);
model.DisplayMode = "ReadOnly";
return View("Index", model);
}
//// GET: Customers/Details/5
//public ActionResult Details(int? id)
//{
// if (id == null)
// {
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
// }
// Customer customer = db.Customers.Find(id);
// if (customer == null)
// {
// return HttpNotFound();
// }
// return View(customer);
//}
//// GET: Customers/Create
//public ActionResult Create()
//{
// return View();
//}
//// POST: Customers/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 = "CustomerID,CustomerName,CustomerContact")] Customer customer)
//{
// if (ModelState.IsValid)
// {
// db.Customers.Add(customer);
// db.SaveChanges();
// return RedirectToAction("Index");
// }
// return View(customer);
//}
//// GET: Customers/Edit/5
//public ActionResult Edit(int? id)
//{
// if (id == null)
// {
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
// }
// Customer customer = db.Customers.Find(id);
// if (customer == null)
// {
// return HttpNotFound();
// }
// return View(customer);
//}
//// POST: Customers/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 = "CustomerID,CustomerName,CustomerContact")] Customer customer)
//{
// if (ModelState.IsValid)
// {
// db.Entry(customer).State = EntityState.Modified;
// db.SaveChanges();
// return RedirectToAction("Index");
// }
// return View(customer);
//}
//// GET: Customers/Delete/5
//public ActionResult Delete(int? id)
//{
// if (id == null)
// {
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
// }
// Customer customer = db.Customers.Find(id);
// if (customer == null)
// {
// return HttpNotFound();
// }
// return View(customer);
//}
//// POST: Customers/Delete/5
//[HttpPost, ActionName("Delete")]
//[ValidateAntiForgeryToken]
//public ActionResult DeleteConfirmed(int id)
//{
// Customer customer = db.Customers.Find(id);
// db.Customers.Remove(customer);
// db.SaveChanges();
// return RedirectToAction("Index");
//}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Views:
Index.cshtml
@model DisplayAllInOnePage.Models.CustomersViewModel
@{
Layout = null;
}
Index
Models:
public class Customer
{
[EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public string CustomerContact { get; set; }
}
public class CustomersViewModel
{
public List
public Customer SelectedCustomer { get; set; }
public string DisplayMode { get; set; }
}
Controllers:
namespace DisplayAllInOnePage.Controllers
{
public class CustomersController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Customers
public ActionResult Index()
{
//List
//Customer customer1 = new Customer();
//customer1.CustomerID = 1;
//customer1.CustomerName = "Name1";
//customer1.CustomerContact = "Contact1";
//Customer customer2 = new Customer();
//customer2.CustomerID = 2;
//customer2.CustomerName = "Name2";
//customer2.CustomerContact = "Contact2";
//var all = from c in db.Customers select c;
//db.Customers.RemoveRange(all);
//db.SaveChanges();
//db.Customers.Add(customer1);
//db.Customers.Add(customer2);
//db.SaveChanges();
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = null;
return View(model);
//return View(db.Customers.ToList());
}
[HttpPost]
public ActionResult New()
{
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = null;
model.DisplayMode = "WriteOnly";
return View("Index", model);
}
[HttpPost]
public ActionResult Insert(Customer obj)
{
db.Customers.Add(obj);
db.SaveChanges();
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = db.Customers.Find(obj.CustomerID);
model.DisplayMode = "ReadOnly";
return View("Index", model);
}
[HttpPost]
public ActionResult Select(int id)
{
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = db.Customers.Find(id);
model.DisplayMode = "ReadOnly";
return View("Index", model);
}
[HttpPost]
public ActionResult Edit(int id)
{
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = db.Customers.Find(id);
model.DisplayMode = "ReadWrite";
return View("Index", model);
}
[HttpPost]
public ActionResult Update(Customer obj)
{
Customer existing = db.Customers.Find(obj.CustomerID);
existing.CustomerName = obj.CustomerName;
existing.CustomerContact = obj.CustomerContact;
db.SaveChanges();
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = existing;
model.DisplayMode = "ReadOnly";
return View("Index", model);
}
[HttpPost]
public ActionResult Delete(int id)
{
Customer existing = db.Customers.Find(id);
db.Customers.Remove(existing);
db.SaveChanges();
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = null;
model.DisplayMode = "";
return View("Index", model);
}
[HttpPost]
public ActionResult Cancel(int id)
{
CustomersViewModel model = new CustomersViewModel();
model.Customers = db.Customers.OrderBy(
m => m.CustomerID).Take(5).ToList();
model.SelectedCustomer = db.Customers.Find(id);
model.DisplayMode = "ReadOnly";
return View("Index", model);
}
//// GET: Customers/Details/5
//public ActionResult Details(int? id)
//{
// if (id == null)
// {
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
// }
// Customer customer = db.Customers.Find(id);
// if (customer == null)
// {
// return HttpNotFound();
// }
// return View(customer);
//}
//// GET: Customers/Create
//public ActionResult Create()
//{
// return View();
//}
//// POST: Customers/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 = "CustomerID,CustomerName,CustomerContact")] Customer customer)
//{
// if (ModelState.IsValid)
// {
// db.Customers.Add(customer);
// db.SaveChanges();
// return RedirectToAction("Index");
// }
// return View(customer);
//}
//// GET: Customers/Edit/5
//public ActionResult Edit(int? id)
//{
// if (id == null)
// {
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
// }
// Customer customer = db.Customers.Find(id);
// if (customer == null)
// {
// return HttpNotFound();
// }
// return View(customer);
//}
//// POST: Customers/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 = "CustomerID,CustomerName,CustomerContact")] Customer customer)
//{
// if (ModelState.IsValid)
// {
// db.Entry(customer).State = EntityState.Modified;
// db.SaveChanges();
// return RedirectToAction("Index");
// }
// return View(customer);
//}
//// GET: Customers/Delete/5
//public ActionResult Delete(int? id)
//{
// if (id == null)
// {
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
// }
// Customer customer = db.Customers.Find(id);
// if (customer == null)
// {
// return HttpNotFound();
// }
// return View(customer);
//}
//// POST: Customers/Delete/5
//[HttpPost, ActionName("Delete")]
//[ValidateAntiForgeryToken]
//public ActionResult DeleteConfirmed(int id)
//{
// Customer customer = db.Customers.Find(id);
// db.Customers.Remove(customer);
// db.SaveChanges();
// return RedirectToAction("Index");
//}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Views:
Index.cshtml
@model DisplayAllInOnePage.Models.CustomersViewModel
@{
Layout = null;
}
List of Customers
value="Insert" formaction="/Customers/new" />
CustomerID
CompanyName
Actions
@foreach (var item in Model.Customers)
{
if (Model.SelectedCustomer != null)
{
if (item.CustomerID == Model.SelectedCustomer.CustomerID)
{
@:
}
else
{
@:
}
}
else
{
@:
}
@item.CustomerID
@item.CustomerName
formaction="/Customers/select/@item.CustomerID"
value="Select" />
formaction="/Customers/delete/@item.CustomerID"
value="Delete" />
@:
}
@{
if (Model.SelectedCustomer != null)
{
if (Model.DisplayMode == "ReadOnly")
{
Html.RenderPartial
("ShowCustomer", Model.SelectedCustomer);
}
if (Model.DisplayMode == "ReadWrite")
{
Html.RenderPartial
("EditCustomer", Model.SelectedCustomer);
}
}
if (Model.DisplayMode == "WriteOnly")
{
Html.RenderPartial("InsertCustomer",
new DisplayAllInOnePage.Models.Customer());
}
}
Create.cshtml
@model DisplayAllInOnePage.Models.Customer
@{
ViewBag.Title = "Create";
}
Create
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
Customer
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.CustomerContact, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.CustomerContact, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerContact, "", new { @class = "text-danger" })
}
@Html.ActionLink("Back to List", "Index")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Delete.cshtml
@model DisplayAllInOnePage.Models.Customer
@{
ViewBag.Title = "Delete";
}
Delete
Are you sure you want to delete this?
Customer
@Html.DisplayNameFor(model => model.CustomerName)
@Html.DisplayFor(model => model.CustomerName)
@Html.DisplayNameFor(model => model.CustomerContact)
@Html.DisplayFor(model => model.CustomerContact)
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
|
@Html.ActionLink("Back to List", "Index")
}
Details.cshtml
@model DisplayAllInOnePage.Models.Customer
@{
ViewBag.Title = "Details";
}
Details
Customer
@Html.DisplayNameFor(model => model.CustomerName)
@Html.DisplayFor(model => model.CustomerName)
@Html.DisplayNameFor(model => model.CustomerContact)
@Html.DisplayFor(model => model.CustomerContact)
@Html.ActionLink("Edit", "Edit", new { id = Model.CustomerID }) |
@Html.ActionLink("Back to List", "Index")
Edit.cshtml
@model DisplayAllInOnePage.Models.Customer
@{
ViewBag.Title = "Edit";
}
Edit
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
Customer
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.CustomerID)
@Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.CustomerContact, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.CustomerContact, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerContact, "", new { @class = "text-danger" })
}
@Html.ActionLink("Back to List", "Index")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
EditCustomer.cshtml
@model DisplayAllInOnePage.Models.Customer
@using (Html.BeginForm("Update", "Home", FormMethod.Post))
{
Customer ID :
@Html.TextBoxFor(m => m.CustomerID,
new { @readonly = "readonly" })
Company Name :
@Html.TextBoxFor(m => m.CustomerName)
Contact Name :
@Html.TextBoxFor(m => m.CustomerContact)
formaction="/Customers/update" />
formaction="/Customers/cancel/@Model.CustomerID" />
}
InsertCustomer.cshtml
@model DisplayAllInOnePage.Models.Customer
@using (Html.BeginForm("Insert", "Home", FormMethod.Post))
{
Customer ID :
@Html.TextBoxFor(m => m.CustomerID)
Customer Name :
@Html.TextBoxFor(m => m.CustomerName)
Contact Name :
@Html.TextBoxFor(m => m.CustomerContact)
formaction="/Customers/insert" />
formaction="/Customers/index" />
}
ShowCustomer.cshtml
@model DisplayAllInOnePage.Models.Customer
@using (Html.BeginForm("Edit", "Home", FormMethod.Post))
{
Customer ID :
@Model.CustomerID
Customer Name :
@Model.CustomerName
Contact Name :
@Model.CustomerContact
formaction="/Customers/edit/@Model.CustomerID" />
formaction="/Customers/index" />
}
No comments:
Post a Comment