-
September 26th, 2014, 10:29 AM
#1
Simple C# MVC CRUD app, using EF5, and SQL '08.
I am working on a test application for a job interview (I'm fresh out of school and applying for a Jr. Dev role) and it is a very simple app (supposed to be). I have 2 controllers, one for "Products" and one for "ProductNotes", both from tables in my db. I have the "Product" controller as the shared layout so when i run the app the "Product" view opens and displays the table with data from the table. I have added a button to that view which i need to redirect to the "ProductNotes" view, but so far I have not been successful. I am using razor syntax. See code below. If anyone could shed some light on this for me I would be grateful as I need to submit this today. Thanks!
ProductController.cs
Code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MadisonResource_TestApp.Controllers
{
public class ProductController : Controller
{
private ProductEntities db = new ProductEntities();
//
// GET: /Product/
public ActionResult Index()
{
return View(db.Products.ToList());
}
//
// GET: /Product/Details/5
public ActionResult Details(int id = 0)
{
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
//
// GET: /Product/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Product/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
//
// GET: /Product/Edit/5
public ActionResult Edit(int id = 0)
{
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
//
// POST: /Product/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
db.Entry(product).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
//
// GET: /Product/Delete/5
public ActionResult Delete(int id = 0)
{
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
//
// POST: /Product/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Product product = db.Products.Find(id);
db.Products.Remove(product);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
Product view(Index.cshtml)
HTML Code:
@model IEnumerable<MadisonResource_TestApp.Product>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
<p>
<input id="Button1" type="button" value="View Notes">
</p>
ProductNotesController.cs
Code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MadisonResource_TestApp.Controllers
{
public class ProductNotesController : Controller
{
private ProductEntities db = new ProductEntities();
//
// GET: /ProductNotes/
public ActionResult Index()
{
var productnotes = db.ProductNotes.Include(p => p.Product);
return View(productnotes.ToList());
}
//
// GET: /ProductNotes/Details/5
public ActionResult Details(int id = 0)
{
ProductNote productnote = db.ProductNotes.Find(id);
if (productnote == null)
{
return HttpNotFound();
}
return View(productnote);
}
//
// GET: /ProductNotes/Create
public ActionResult Create()
{
ViewBag.ProductID = new SelectList(db.Products, "ID", "Name");
return View();
}
//
// POST: /ProductNotes/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ProductNote productnote)
{
if (ModelState.IsValid)
{
db.ProductNotes.Add(productnote);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", productnote.ProductID);
return View(productnote);
}
//
// GET: /ProductNotes/Edit/5
public ActionResult Edit(int id = 0)
{
ProductNote productnote = db.ProductNotes.Find(id);
if (productnote == null)
{
return HttpNotFound();
}
ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", productnote.ProductID);
return View(productnote);
}
//
// POST: /ProductNotes/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ProductNote productnote)
{
if (ModelState.IsValid)
{
db.Entry(productnote).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", productnote.ProductID);
return View(productnote);
}
//
// GET: /ProductNotes/Delete/5
public ActionResult Delete(int id = 0)
{
ProductNote productnote = db.ProductNotes.Find(id);
if (productnote == null)
{
return HttpNotFound();
}
return View(productnote);
}
//
// POST: /ProductNotes/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
ProductNote productnote = db.ProductNotes.Find(id);
db.ProductNotes.Remove(productnote);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
ProductNotes view (Index.cshtml)
HTML Code:
@model IEnumerable<MadisonResource_TestApp.ProductNote>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Product.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.NoteText)
</th>
<th>
@Html.DisplayNameFor(model => model.CreateDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Archived)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Product.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.NoteText)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreateDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Archived)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|