Hands-On Design Patterns With C# and .NET Core by Gaurav Aroraa & Jeffrey Chilberto

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



Copyright Disclaimer:
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.