Язык программирования C#9 и платформа .NET5 - Страница 641
Изменить размер шрифта:
public async Task<IActionResult> ByMake(int makeId, string makeName)
{
ViewBag.MakeName = makeName;
return View(await _serviceWrapper.GetCarsByMakeAsync(makeId));
}
[HttpGet("{id?}")]
public async Task<IActionResult> Details(int? id)
{
if (!id.HasValue)
{
return BadRequest();
}
var car = await GetOneCarAsync(id);
if (car == null)
{
return NotFound();
}
return View(car);
}
[HttpGet]
public async Task<IActionResult> Create()
{
ViewData["MakeId"] = await GetMakesAsync();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Car car)
{
if (ModelState.IsValid)
{
await _serviceWrapper.AddCarAsync(car);
return RedirectToAction(nameof(Index));
}
ViewData["MakeId"] = await GetMakesAsync();
return View(car);
}
[HttpGet("{id?}")]
public async Task<IActionResult> Edit(int? id)
{
var car = await GetOneCarAsync(id);
if (car == null)
{
return NotFound();
}
ViewData["MakeId"] = await GetMakesAsync();
return View(car);
}
[HttpPost("")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, Car car)
{
if (id != car.Id)
{
return BadRequest();
}
if (ModelState.IsValid)
{
await _serviceWrapper.UpdateCarAsync(id,car);
return RedirectToAction(nameof(Index));
}
ViewData["MakeId"] = await GetMakesAsync();
return View(car);
}
[HttpGet("{id?}")]
public async Task<IActionResult> Delete(int? id)
{
var car = await GetOneCarAsync(id);
if (car == null)
{
return NotFound();
}
return View(car);
}
[HttpPost("")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Delete(int id, Car car)
{
await _serviceWrapper.DeleteCarAsync(id,car);
return RedirectToAction(nameof(Index));
}
Обновление компонента представления
В текущий момент внутри компонента представления
MenuViewComponent
Invoke()
using System.Linq;
using System.Threading.Tasks;
using AutoLot.Dal.Repos.Interfaces;
using AutoLot.Services.ApiWrapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;
namespace AutoLot.Mvc.ViewComponents
{
public class MenuViewComponent : ViewComponent
{
private readonly IApiServiceWrapper _serviceWrapper;
public MenuViewComponent(IApiServiceWrapper serviceWrapper)
{
_serviceWrapper = serviceWrapper;