Asp.net MVC CRUD Operation
March 20, 2015 Leave a comment
using MvcLastAttempt.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace MvcLastAttempt.Controllers
{
public class HelpController : Controller
{
//
// GET: /Help/
private MarutiEntities db = new MarutiEntities();
public ActionResult Index()
{
var products = db.Products.Include(p => p.Category);
return View(db.Products.ToList());
}
public ActionResult Create()
{
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName");
return View();
}
[HttpPost]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
return View(product);
}
public ActionResult Edit(int id = 0)
{
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
return View(product);
}
//
// POST: /Main/Edit/5
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
db.Entry(product).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
return View(product);
}
public ActionResult Delete(int id = 0)
{
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
//
// POST: /Main/Delete/5
[HttpPost, ActionName("Delete")]
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);
}
}
}
VIEW
@model MvcLastAttempt.Models.Product
@{
Layout = null;
}
<script src=''>
Create
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
Product
@Html.ValidationMessageFor(model => model.CategoryID)
@Html.ValidationMessageFor(model => model.ProductName)
@Html.ValidationMessageFor(model => model.Description)
}
--Edit
@model MvcLastAttempt.Models.Product
@{
Layout = null;
}
Edit
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
Product
@Html.HiddenFor(model => model.ProductID)
@Html.ValidationMessageFor(model => model.CategoryID)
@Html.ValidationMessageFor(model => model.ProductName)
@Html.ValidationMessageFor(model => model.Description)
}
--Delete
@model MvcLastAttempt.Models.Product
@{
Layout = null;
}
Delete
Are you sure you want to delete this?
Product
@using (Html.BeginForm()) {
|
@Html.ActionLink("Back to List", "Index")
}
--List
@model IEnumerable
@{
Layout = null;
}
Index
@Html.ActionLink("Create New", "Create")
@Html.DisplayNameFor(model => model.Category.CategoryName) | @Html.DisplayNameFor(model => model.ProductName) | @Html.DisplayNameFor(model => model.Description) | |
---|---|---|---|
@Html.DisplayFor(modelItem => item.Category.CategoryName) | @Html.DisplayFor(modelItem => item.ProductName) | @Html.DisplayFor(modelItem => item.Description) |
@Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) | @Html.ActionLink("Details", "Details", new { id=item.ProductID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ProductID }) |