For a school project I made a very simple CRUD application. Nothing fancy. I have product table that displays a title, description and a price. But I would like to include images as well. I am new to MVC, and while I have tried adding images to an application before, I have never done it in and MVC and Entity eviroment. In previous projects, I remember using WebImage and using a jpg file stored in an image folder that I created. I DID create an image folder for this project too with some jpg files in it, but I don't know where to go from here.
My product.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace ProductApps.Models
{
public class Product
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string ImageName { get; set; }
}
}
My controller
using ProductApps.Context;
using ProductApps.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
namespace ProductApps.Controllers
{
public class ProductController : Controller
{
private ProductContext db = new ProductContext();
// GET: Product
public ActionResult Index()
{
return View(db.Products.ToList());
}
// GET: Product/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: Product/Create
[HttpGet]
public ActionResult Create()
{
return View();
}
// POST: Product/Create
[HttpPost]
public ActionResult Create(Product product)
{
try
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
catch
{
return View();
}
}
// GET: Product/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: Product/Edit/5
[HttpPost]
public ActionResult Edit(Product product)
{
try
{
if (ModelState.IsValid)
{
db.Entry(product).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
catch
{
return View();
}
}
// GET: Product/Delete/5
public ActionResult Delete(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: Product/Delete/5
[HttpPost]
public ActionResult Delete(int? id, Product prod)
{
try
{
Product product = new Product();
if (ModelState.IsValid)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
product = db.Products.Find(id);
if (product == null)
return HttpNotFound();
db.Products.Remove(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
catch
{
return View();
}
}
}
}