• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Discounting Products in ASP.NET

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.

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
    }
}

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
            }
        }
    }
 
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.
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
}
}


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
}
}
}
 

roxanefabin786

New Coder
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.
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
}
}
Product Manager Controller:
public class ProductManagerController : Controller
{
IRepository<Product> context;
IRepository<ProductCategory> productCategories;
  1. public ProductManagerController(IRepository<Product> productContext, IRepository<ProductCategory> productCategoryContext)
  2. {
  3. context = productContext;
  4. productCategories = productCategoryContext;
  5. }

  6. //GET: ProductManager
  7. //shows the list of all products
  8. public ActionResult Index()
  9. {
  10. List<Product> products = context.Collection().ToList();
  11. return View(products); //returns view of all products
  12. }

  13. //allows admin to create a new product
  14. public ActionResult Create()
  15. {
  16. ProductManagerViewModel viewModel = new ProductManagerViewModel();
  17. viewModel.Product = new Product();
  18. viewModel.ProductCategories = productCategories.Collection();
  19. return View(viewModel);
  20. }

  21. [HttpPost] //called once user hits create
  22. public ActionResult Create(Product product, HttpPostedFileBase file)
  23. {
  24. if (!ModelState.IsValid)
  25. {
  26. return View(product); //returns the user if not valid
  27. }
  28. else
  29. {
  30. if (file != null)
  31. {
  32. product.Image = product.Id + Path.GetExtension(file.FileName);
  33. file.SaveAs(Server.MapPath("//Content//ProductImages//") + product.Image);
  34. }
  35. context.Insert(product); //insert into products
  36. context.Commit(); //commit the saved changes

  37. return RedirectToAction("Index"); //return user to products page
  38. }
  39. }

  40. //allows admin to edit products
  41. public ActionResult Edit(string id)
  42. {
  43. Product product = context.Find(id); //finds the product

  44. if(product == null)
  45. {
  46. return HttpNotFound(); //product not found
  47. }
  48. else
  49. {
  50. ProductManagerViewModel viewModel = new ProductManagerViewModel();
  51. viewModel.Product = product;
  52. viewModel.ProductCategories = productCategories.Collection();
  53. return View(viewModel);
  54. }
  55. }

  56. [HttpPost] //called when user hits edit
  57. public ActionResult Edit(Product product, string id, HttpPostedFileBase file)
  58. {
  59. Product productToEdit = context.Find(id); //find the product

  60. if (productToEdit == null)
  61. {
  62. return HttpNotFound(); //product could not be found
  63. }
  64. else
  65. {
  66. if (!ModelState.IsValid)
  67. {
  68. return View(product);
  69. }

  70. if (file != null)
  71. {
  72. productToEdit.Image = product.Id + Path.GetExtension(file.FileName);
  73. file.SaveAs(Server.MapPath("//Content//ProductImages//") + productToEdit.Image);
  74. }

  75. //changes details about products
  76. productToEdit.Category = product.Category;
  77. productToEdit.Description = product.Description;
  78. productToEdit.Name = product.Name;
  79. productToEdit.Price = product.Price;
  80. productToEdit.StockLevel = product.StockLevel;

  81. context.Commit(); //commit the changes

  82. return RedirectToAction("Index"); //return the user
  83. }
  84. }

  85. //allows user to delete a product
  86. public ActionResult Delete(string id)
  87. {
  88. Product productToDelete = context.Find(id); //finds the product

  89. if(productToDelete == null)
  90. {
  91. return HttpNotFound(); //product could not be found
  92. }
  93. else
  94. {
  95. return View(productToDelete); //allows user to delete product
  96. }
  97. }

  98. [HttpPost] //called when user hits delete
  99. [ActionName("Delete")]
  100. public ActionResult ConfirmDelete(string id)
  101. {
  102. Product productToDelete = context.Find(id); //finds the product

  103. if (productToDelete == null)
  104. {
  105. return HttpNotFound(); //product could not be found
  106. }
  107. else
  108. {
  109. context.Delete(id); //delete the product
  110. context.Commit(); //commits the changes
  111. return RedirectToAction("Index"); //returns the user
  112. }
  113. }
  114. }
 

Top