question

ashleighhalliday-7203 avatar image
0 Votes"
ashleighhalliday-7203 asked LanHuang-MSFT answered

Discounting Products in ASP.NET

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
         }
     }
 }
dotnet-csharpdotnet-aspnet-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I was wondering where I should implement the Discount Price feature.

You did not explain how your discount feature works. I'll assume select user accounts receive a percent discount on all items in inventory. This "discount percentage" is associated with the user's login and the value should be stored in a table related to the user account table. Form there it is a simple calculation to find the discount value.

 var discountValue = actualValue * (1 - userDiscountPercentage);



0 Votes 0 ·

1 Answer

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @ashleighhalliday-7203,
Do you have set up identity verification when you log in?
If the discount only be showed for the member, you could implement the Discount Price feature in the member part.
And What' the effect of your controller of 'members only' ?
Best regards,
Lan Huang


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.