Hands-On Design Patterns With C# and .NET Core by Gaurav Aroraa & Jeffrey Chilberto
Author:Gaurav Aroraa & Jeffrey Chilberto [Aroraa, Gaurav & Chilberto, Jeffrey]
Language: eng
Format: epub
Tags: COM051240 - COMPUTERS / Software Development and Engineering / Systems Analysis and Design, COM051310 - COMPUTERS / Programming Languages / C#, COM051230 - COMPUTERS / Software Development and Engineering / General
ISBN: 9781789133646
Google: o9EDuAEACAAJ
Publisher: Packt Publishing
Published: 2019-07-05T23:27:51.667359+00:00
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(Guid id, [FromBody] Product product)
{
try
{
_repositry.UpdateProduct(product);
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
The first action method of the previous code gets a Product based on the ID and returns a View. The second action method takes the data from the view and updates the requested Product based on its ID:
public IActionResult Delete(Guid id) => View(_repositry.GetProduct(id));
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Delete(Guid id, [FromBody] Product product)
{
try
{
_repositry.RemoveProduct(product);
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
Finally, the previous code represents the Delete operation from our CRUD operations. It also has two action methods; one retrieves the data from the repository and serves it to the view, and another takes the data request and deletes the specific Product based on its ID.
CategoryController is responsible for all the operations of the Product category. Add the following code to the controller, it represents the CategoryController, where we have used dependency injections to initialize our IInventoryRepository:
public class CategoryController: Controller
{
private readonly IInventoryRepositry _inventoryRepositry;
public CategoryController(IInventoryRepositry inventoryRepositry) => _inventoryRepositry = inventoryRepositry;
//code omitted
}
The following code contains two action methods. The first gets a list of categories, and the second is a specific category based on its unique ID:
public IActionResult Index() => View(_inventoryRepositry.GetCategories());
public IActionResult Details(Guid id) => View(_inventoryRepositry.GetCategory(id));
The following code is for the Get and the Post request to create a new category in the system:
public IActionResult Create() => View();
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create([FromBody] Category category)
{
try
{
_inventoryRepositry.AddCategory(category);
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Exploring Deepfakes by Bryan Lyon and Matt Tora(7735)
Robo-Advisor with Python by Aki Ranin(7630)
Offensive Shellcode from Scratch by Rishalin Pillay(6110)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(5030)
Ego Is the Enemy by Ryan Holiday(4958)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4438)
Python for ArcGIS Pro by Silas Toms Bill Parker(4186)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3892)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3630)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3285)
Speed Up Your Python with Rust by Maxwell Flitton(3231)
Liar's Poker by Michael Lewis(3227)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3195)
Extreme DAX by Michiel Rozema & Henk Vlootman(3172)
Agile Security Operations by Hinne Hettema(3124)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3109)
Essential Cryptography for JavaScript Developers by Alessandro Segala(3083)
Cryptography Algorithms by Massimo Bertaccini(3002)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2983)
