ashleigh14
New Coder
Hi.
I am creating a simple application in ASP.NET with some products in a database. I have a specific 'members only' controller where I want to allow members to receive discounts on the products listed on the website.
I have posted my product class below where I am thinking I need to add in the DiscountPrice attribute and the code for my product manager controller which contains all of the basic index, create, edit, delete.... I have also created a separate Members Only Controller which is where I am looking to display the discounted prices to only those who are members of the website.
I was wondering where I should implement the Discount Price feature. I need to display all of the products in my separate members only page but have those to feature the discounted prices. Is anyone able to provide any guidance if I am along the right lines or could provide any help for what I need to do from here? I am new here so ask any questions if I have done something wrong or this is not clear.
[CODE lang="csharp" title="Product Class"]public class Product : BaseEntity
{
[StringLength(50)]
[DisplayName("Product Name")]
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public string Image { get; set; }
public int StockLevel { get; set; }
//public decimal DiscountPrice { get; set; } //the discount price for members/premium
}
}[/CODE]
[CODE lang="csharp" title="Product Manager Controller"]public class ProductManagerController : Controller
{
IRepository<Product> context;
IRepository<ProductCategory> productCategories;
public ProductManagerController(IRepository<Product> productContext, IRepository<ProductCategory> productCategoryContext)
{
context = productContext;
productCategories = productCategoryContext;
}
//GET: ProductManager
//shows the list of all products
public ActionResult Index()
{
List<Product> products = context.Collection().ToList();
return View(products); //returns view of all products
}
//allows admin to create a new product
public ActionResult Create()
{
ProductManagerViewModel viewModel = new ProductManagerViewModel();
viewModel.Product = new Product();
viewModel.ProductCategories = productCategories.Collection();
return View(viewModel);
}
[HttpPost] //called once user hits create
public ActionResult Create(Product product, HttpPostedFileBase file)
{
if (!ModelState.IsValid)
{
return View(product); //returns the user if not valid
}
else
{
if (file != null)
{
product.Image = product.Id + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("//Content//ProductImages//") + product.Image);
}
context.Insert(product); //insert into products
context.Commit(); //commit the saved changes
return RedirectToAction("Index"); //return user to products page
}
}
//allows admin to edit products
public ActionResult Edit(string id)
{
Product product = context.Find(id); //finds the product
if(product == null)
{
return HttpNotFound(); //product not found
}
else
{
ProductManagerViewModel viewModel = new ProductManagerViewModel();
viewModel.Product = product;
viewModel.ProductCategories = productCategories.Collection();
return View(viewModel);
}
}
[HttpPost] //called when user hits edit
public ActionResult Edit(Product product, string id, HttpPostedFileBase file)
{
Product productToEdit = context.Find(id); //find the product
if (productToEdit == null)
{
return HttpNotFound(); //product could not be found
}
else
{
if (!ModelState.IsValid)
{
return View(product);
}
if (file != null)
{
productToEdit.Image = product.Id + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("//Content//ProductImages//") + productToEdit.Image);
}
//changes details about products
productToEdit.Category = product.Category;
productToEdit.Description = product.Description;
productToEdit.Name = product.Name;
productToEdit.Price = product.Price;
productToEdit.StockLevel = product.StockLevel;
context.Commit(); //commit the changes
return RedirectToAction("Index"); //return the user
}
}
//allows user to delete a product
public ActionResult Delete(string id)
{
Product productToDelete = context.Find(id); //finds the product
if(productToDelete == null)
{
return HttpNotFound(); //product could not be found
}
else
{
return View(productToDelete); //allows user to delete product
}
}
[HttpPost] //called when user hits delete
[ActionName("Delete")]
public ActionResult ConfirmDelete(string id)
{
Product productToDelete = context.Find(id); //finds the product
if (productToDelete == null)
{
return HttpNotFound(); //product could not be found
}
else
{
context.Delete(id); //delete the product
context.Commit(); //commits the changes
return RedirectToAction("Index"); //returns the user
}
}
}[/CODE]
I am creating a simple application in ASP.NET with some products in a database. I have a specific 'members only' controller where I want to allow members to receive discounts on the products listed on the website.
I have posted my product class below where I am thinking I need to add in the DiscountPrice attribute and the code for my product manager controller which contains all of the basic index, create, edit, delete.... I have also created a separate Members Only Controller which is where I am looking to display the discounted prices to only those who are members of the website.
I was wondering where I should implement the Discount Price feature. I need to display all of the products in my separate members only page but have those to feature the discounted prices. Is anyone able to provide any guidance if I am along the right lines or could provide any help for what I need to do from here? I am new here so ask any questions if I have done something wrong or this is not clear.
[CODE lang="csharp" title="Product Class"]public class Product : BaseEntity
{
[StringLength(50)]
[DisplayName("Product Name")]
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public string Image { get; set; }
public int StockLevel { get; set; }
//public decimal DiscountPrice { get; set; } //the discount price for members/premium
}
}[/CODE]
[CODE lang="csharp" title="Product Manager Controller"]public class ProductManagerController : Controller
{
IRepository<Product> context;
IRepository<ProductCategory> productCategories;
public ProductManagerController(IRepository<Product> productContext, IRepository<ProductCategory> productCategoryContext)
{
context = productContext;
productCategories = productCategoryContext;
}
//GET: ProductManager
//shows the list of all products
public ActionResult Index()
{
List<Product> products = context.Collection().ToList();
return View(products); //returns view of all products
}
//allows admin to create a new product
public ActionResult Create()
{
ProductManagerViewModel viewModel = new ProductManagerViewModel();
viewModel.Product = new Product();
viewModel.ProductCategories = productCategories.Collection();
return View(viewModel);
}
[HttpPost] //called once user hits create
public ActionResult Create(Product product, HttpPostedFileBase file)
{
if (!ModelState.IsValid)
{
return View(product); //returns the user if not valid
}
else
{
if (file != null)
{
product.Image = product.Id + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("//Content//ProductImages//") + product.Image);
}
context.Insert(product); //insert into products
context.Commit(); //commit the saved changes
return RedirectToAction("Index"); //return user to products page
}
}
//allows admin to edit products
public ActionResult Edit(string id)
{
Product product = context.Find(id); //finds the product
if(product == null)
{
return HttpNotFound(); //product not found
}
else
{
ProductManagerViewModel viewModel = new ProductManagerViewModel();
viewModel.Product = product;
viewModel.ProductCategories = productCategories.Collection();
return View(viewModel);
}
}
[HttpPost] //called when user hits edit
public ActionResult Edit(Product product, string id, HttpPostedFileBase file)
{
Product productToEdit = context.Find(id); //find the product
if (productToEdit == null)
{
return HttpNotFound(); //product could not be found
}
else
{
if (!ModelState.IsValid)
{
return View(product);
}
if (file != null)
{
productToEdit.Image = product.Id + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("//Content//ProductImages//") + productToEdit.Image);
}
//changes details about products
productToEdit.Category = product.Category;
productToEdit.Description = product.Description;
productToEdit.Name = product.Name;
productToEdit.Price = product.Price;
productToEdit.StockLevel = product.StockLevel;
context.Commit(); //commit the changes
return RedirectToAction("Index"); //return the user
}
}
//allows user to delete a product
public ActionResult Delete(string id)
{
Product productToDelete = context.Find(id); //finds the product
if(productToDelete == null)
{
return HttpNotFound(); //product could not be found
}
else
{
return View(productToDelete); //allows user to delete product
}
}
[HttpPost] //called when user hits delete
[ActionName("Delete")]
public ActionResult ConfirmDelete(string id)
{
Product productToDelete = context.Find(id); //finds the product
if (productToDelete == null)
{
return HttpNotFound(); //product could not be found
}
else
{
context.Delete(id); //delete the product
context.Commit(); //commits the changes
return RedirectToAction("Index"); //returns the user
}
}
}[/CODE]