public DbSet
public DbSet
//public DbSet
}
}
public class Product
{
[HiddenInput(DisplayValue = false)]
public int ProductID { get; set; }
[Required(ErrorMessage = "Please enter a picture name")]
[Display(Name = "项目编号")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter a description")]
[Display(Name = "项目名称")]
public string Description { get; set; }
[Required]
[Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
[Display(Name = "项目估价")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Please specify a category")]
[Display(Name ="主要参数")]
public string Category { get; set; }
//[Display(Name = "设计图纸")]
//public byte[] ImageData { get; set; }
//[HiddenInput(DisplayValue = false)]//会让改属性在编辑的时候不显示出来。
//[Display(Name = "上传图纸")]
//public string ImageType{ get; set; }
public virtual ICollection
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using KindEditor.Models;
namespace KindEditor.Controllers
{
public class ProductsController : Controller
{
private ArticleDBContext db = new ArticleDBContext();
// GET: Products
public ActionResult Index()
{
return View(db.Products.ToList());
}
// GET: Products/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
// GET: Products/Create
public ActionResult Create()
{
return View();
}
// POST: Products/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 = "ProductID,Name,Description,Price,Category,ImageData,ImageType")] Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
// GET: Products/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
// POST: Products/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 = "ProductID,Name,Description,Price,Category,ImageData,ImageType")] Product product)
//{
// if (ModelState.IsValid)
// {
// db.Entry(product).State = EntityState.Modified;
// db.SaveChanges();
// return RedirectToAction("Index");
// }
// return View(product);
//}
[HttpPost]
public ActionResult Edit([Bind(Include = "ProductID,Name,Description,Price,Category,ImageData,ImageType")] Product product, HttpPostedFilebase image){
if (ModelState.IsValid) {
if (image != null) {
product.ImageType = image.ContentType;//获取图片类型
product.ImageData = new byte[image.ContentLength];//新建一个长度等于图片大小的二进制地址
image.InputStream.Read(product.ImageData, 0, image.ContentLength);//将image读取到ImageData中 // save the product
}
else
{
var productTryupdat = db.Products
.wher(i => i.ProductID == product.ProductID)
.Single();
if (TryUpdateModel(productTryUpdate, "", new string[] { "ProductID", "Name", "Description", "Price", "Category" }))
{
db.SaveChanges();
return RedirectToAction("Index");
} //更新一下 保存模型。
}
db.Entry(product).State = EntityState.Modified;
db.SaveChanges();
// add a message to the viewbag
TempData["message"] = string.Format("{0} has been saved", product.Name);
// return the user to the list
return RedirectToAction("Index");
}
else
{
// there is something wrong with the data values
return View(product);
}
}
public FileContentResult GetImage(int productId)
{
Product prod = db.Products.FirstOrDefault(p => p.ProductID == productId);
if (prod != null)
{
return File(prod.ImageData, prod.ImageType);
}
else
{
return null;
}
}
// GET: Products/delet/5
public ActionResult delet(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
// POST: Products/delet/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)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
@model KindEditor.Models.Product
@{
ViewBag.Title = "Edit";
}
编辑内容
@using (Html.BeginForm("Edit", "Products", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
勘察设计
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ProductID)
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.ImageData, htmlAttributes: new { @class = "control-label col-md-2" })
@if (Model.ImageData == null)
{
@:None
}
else
{
}
@Html.LabelFor(model => model.ImageType, htmlAttributes: new { @class = "control-label col-md-2" })
}
@Html.Actionlink("Back to List", "Index")