Commit c9b66f17 authored by TTrueBenji's avatar TTrueBenji

Добавить функционал редактирования.

***
- реализовал редактирование смартвона.
- вынес форму редактирования и создания смартфона в PartialView.
***
parent 320b1ea3
...@@ -26,45 +26,6 @@ namespace PhoneStore.Controllers ...@@ -26,45 +26,6 @@ namespace PhoneStore.Controllers
return View(); return View();
} }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return PhysicalFile(filePath, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// byte[] bytes = System.IO.File.ReadAllBytes(filePath);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(bytes, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// byte[] bytes = System.IO.File.ReadAllBytes(filePath);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(bytes, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// FileStream stream = new FileStream(filePath, FileMode.Open);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(stream, fileType, fileName);
// }
[HttpGet] [HttpGet]
public IActionResult Test() public IActionResult Test()
{ {
...@@ -77,7 +38,7 @@ namespace PhoneStore.Controllers ...@@ -77,7 +38,7 @@ namespace PhoneStore.Controllers
[ActionName("Test2")] [ActionName("Test2")]
public string Test(Phone phone) public string Test(Phone phone)
{ {
return "tets"; return null;
} }
[HttpGet] [HttpGet]
......
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PhoneStore.Models; using PhoneStore.Models;
namespace PhoneStore.Controllers namespace PhoneStore.Controllers
{ {
public class PhoneController : Controller public class PhonesController : Controller
{ {
private readonly MobileContext _db; private readonly MobileContext _db;
public PhoneController(MobileContext db) public PhonesController(MobileContext db)
{ {
_db = db; _db = db;
} }
...@@ -22,13 +22,13 @@ namespace PhoneStore.Controllers ...@@ -22,13 +22,13 @@ namespace PhoneStore.Controllers
public IActionResult Index() public IActionResult Index()
{ {
List<Phone> phones = _db.Phones.ToList(); List<Phone> phones = _db.Phones.ToList();
return View(phones); return View(phones);
} }
[HttpGet] [HttpGet]
public IActionResult Create() public IActionResult Create()
{ {
return View(); return View();
} }
...@@ -39,5 +39,29 @@ namespace PhoneStore.Controllers ...@@ -39,5 +39,29 @@ namespace PhoneStore.Controllers
_db.SaveChanges(); _db.SaveChanges();
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
[HttpGet]
public IActionResult Edit(int phoneId)
{
var phone = _db.Phones.FirstOrDefault(p => p.Id == phoneId);
if (phone is null)
{
return BadRequest();
}
return View(phone);
}
[HttpPost]
public IActionResult Edit(Phone phone)
{
if (phone is null)
{
return BadRequest();
}
_db.Phones.Update(phone);
_db.SaveChanges();
return RedirectToAction("Index");
}
} }
} }
\ No newline at end of file
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Pages" /> <Folder Include="Phones" />
</ItemGroup> </ItemGroup>
</Project> </Project>
...@@ -60,7 +60,6 @@ namespace PhoneStore ...@@ -60,7 +60,6 @@ namespace PhoneStore
else else
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts(); app.UseHsts();
} }
......
...@@ -8,20 +8,11 @@ ...@@ -8,20 +8,11 @@
<h2>Заполните форму для добавления устройства</h2> <h2>Заполните форму для добавления устройства</h2>
<div class="row"> <div class="row">
<div class="phone_add_form col-md-6"> <div class="phone_add_form col-md-6">
<form asp-action="Create" asp-controller="Phone" method="post"> <form asp-action="Create" asp-controller="Phones" method="post">
<div class="form_row"> <div class="form_row">
<label for="name"> @{
Наименование await Html.RenderPartialAsync("PartialViews/PhoneFormPartialView");
<input id="name" type="text" asp-for="Name"> }
</label>
<label for="company">
Производитель
<input id="company" type="text" asp-for="Company">
</label>
<label for="price">
Стоимость
<input id="price" type="text" asp-for="Price">
</label>
<button class="btn btn-outline-warning">Отправить</button> <button class="btn btn-outline-warning">Отправить</button>
</div> </div>
</form> </form>
......
@model Phone
@{
ViewBag.Title = "Редактирование";
Layout = "_Layout";
}
<div class="row">
<div class="col-md-6">
<form asp-action="Edit" method="post">
@{
await Html.RenderPartialAsync("PartialViews/PhoneFormPartialView");
}
<input type="text" hidden asp-for="Id">
<button type="submit"
class="btn btn-outline-warning">Изменить</button>
</form>
</div>
</div>
@model List<Phone> @model List<Phone>
@{ @{
ViewBag.Title = "title"; ViewBag.Title = @"title";
Layout = "_Layout"; Layout = "_Layout";
} }
<a asp-action="Create" asp-controller="Phones">Добавить устройство</a>
<a asp-action="Create" asp-controller="Phone">Добавить устройство</a>
@* https://localhost:5001/Phone/Create method GET*@
@if (Model.Count == 0) @if (Model.Count == 0)
{ {
<h2>Список пуст.</h2> <h2>Список пуст.</h2>
...@@ -46,8 +44,17 @@ else ...@@ -46,8 +44,17 @@ else
Заказать Заказать
</a> </a>
</td> </td>
<td>
<a asp-route-phoneId="@phone.Id"
asp-action="Edit"
asp-controller="Phones"
class="btn btn-outline-warning">
Изменить
</a>
</td>
</tr> </tr>
} }
</table> </table>
} }
@model Phone
<label for="name">
Наименование
<input id="name" type="text" asp-for="Name">
</label>
<label for="company">
Производитель
<input id="company" type="text" asp-for="Company">
</label>
<label for="price">
Стоимость
<input id="price" type="text" asp-for="Price">
</label>
\ No newline at end of file
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a> <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Phone" asp-action="Index">Смартфоны</a> <a class="nav-link text-dark" asp-area="" asp-controller="Phones" asp-action="Index">Смартфоны</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Orders" asp-action="Index">Заказы</a> <a class="nav-link text-dark" asp-area="" asp-controller="Orders" asp-action="Index">Заказы</a>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment