Добавил считывание с файла

parent d3e8bf8d
using System; using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Store.Models; using Store.Models;
...@@ -7,45 +6,26 @@ namespace Store.Controllers ...@@ -7,45 +6,26 @@ namespace Store.Controllers
{ {
public class ProductController : Controller public class ProductController : Controller
{ {
public IActionResult Index() private ProductContext _context;
{
List<Product> products = new List<Product>
{
new Product
{
Id = Guid.NewGuid().ToString(),
Name = "Asus A234",
Amount = 100,
Brand = "Asus",
Category = "TV"
},
new Product
{
Id = Guid.NewGuid().ToString(),
Name = "Asus A674",
Amount = 150,
Brand = "Asus",
Category = "TV"
}
};
ViewBag.products = products; public ProductController(ProductContext context)
{
_context = context;
}
return View(); public IActionResult Index()
{
return View(_context.Products);
} }
public IActionResult Details(string id) public IActionResult Details(string id)
{ {
return View(new Product Product product = _context.Products.FirstOrDefault(p => p.Id == id);
{ ViewBag.product = product;
Id = Guid.NewGuid().ToString(), return View();
Name = "Asus A674",
Amount = 150,
Brand = "Asus",
Category = "TV"
});
} }
[ActionName("CreateProduct")]
[HttpGet] [HttpGet]
public IActionResult Create() public IActionResult Create()
{ {
...@@ -55,7 +35,8 @@ namespace Store.Controllers ...@@ -55,7 +35,8 @@ namespace Store.Controllers
[HttpPost] [HttpPost]
public IActionResult Create(Product product) public IActionResult Create(Product product)
{ {
_context.Products.Add(product);
_context.SaveChanges();
return View("Details", product); return View("Details", product);
} }
} }
......
@model Store.Models.Product
@{ @{
Layout = "_MainLayout"; Layout = "_MainLayout";
} }
...@@ -8,22 +9,27 @@ ...@@ -8,22 +9,27 @@
<form action="/Product/Create" method="post" > <form action="/Product/Create" method="post" >
<div class="form-group"> <div class="form-group">
<label for="">Name</label> <label for="">Name</label>
<input type="text" class="form-control" id="Name" name="Name" placeholder="Enter name"> <input type="text" class="form-control" value="@Model.Name" id="Name" name="Name" placeholder="Enter name">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="">Category</label> <label for="">Category</label>
<input type="text" class="form-control" id="Category" name="Category" placeholder="Enter category"> <input type="text" class="form-control" value="@Model.Category" id="Category" name="Category" placeholder="Enter category">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="">Brand</label> <label for="">Brand</label>
<input type="text" class="form-control" id="Brand" name="Brand" placeholder="Enter brand"> <input type="text" class="form-control" value="@Model.Brand" id="Brand" name="Brand" placeholder="Enter brand">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="">Amount</label> <label for="">Amount</label>
<input type="text" class="form-control" id="Amount" name="Amount" placeholder="Enter amount"> <input type="text" class="form-control" value="@Model.Amount" id="Amount" name="Amount" placeholder="Enter amount">
</div>
<div class="form-group">
<label for="">Description</label>
<input type="text" class="form-control" value="@Model.Description" id="Description" name="Description" placeholder="Enter amount">
</div> </div>
<button type="submit" class="btn btn-primary">Create</button> <button type="submit" class="btn btn-primary">Create</button>
......
@model Store.Models.Product
@{ @{
Layout = "_MainLayout"; Layout = "_MainLayout";
} }
...@@ -9,19 +7,19 @@ ...@@ -9,19 +7,19 @@
<tbody> <tbody>
<tr> <tr>
<td>Name:</td> <td>Name:</td>
<td>@Model.Name</td> <td>@ViewBag.product.Name</td>
</tr> </tr>
<tr> <tr>
<td>Category:</td> <td>Category:</td>
<td>@Model.Category</td> <td>@ViewBag.product.Category</td>
</tr> </tr>
<tr> <tr>
<td>Brand:</td> <td>Brand:</td>
<td>@Model.Brand</td> <td>@ViewBag.product.Brand</td>
</tr> </tr>
<tr> <tr>
<td>Amount:</td> <td>Amount:</td>
<td>@Model.Amount</td> <td>@ViewBag.product.Amount</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
@using Store.Models @using Store.Models
@model IEnumerable<Store.Models.Product>
@{ @{
Layout = "_MainLayout"; Layout = "_MainLayout";
} }
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach (Product product in ViewBag.products) @foreach (Product product in Model)
{ {
<tr> <tr>
<th scope="row">@product.Id</th> <th scope="row">@product.Id</th>
......
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