Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Apka1.Models;
  6. using Apka1.ViewModels;
  7. using Microsoft.AspNetCore.Mvc;
  8.  
  9. namespace Apka1.Controllers
  10. {
  11.    
  12.     public class HomeController : Controller
  13.     {
  14.         private readonly IArticleRepository _articleRepository;
  15.         public HomeController(IArticleRepository articleRepository)
  16.         {
  17.             _articleRepository = articleRepository;
  18.         }
  19.         [Route("")]
  20.         public IActionResult Index()
  21.         {
  22.             var articles = _articleRepository.GetAllArticles().OrderBy(a => a.Capacity);
  23.             var homeViewModel = new HomeViewModel
  24.             {
  25.                 Articles = articles.ToList()
  26.             };
  27.             return View(homeViewModel);
  28.         }
  29.         public IActionResult Create()
  30.         {
  31.            
  32.             return View();
  33.         }
  34.         [HttpPost]
  35.         public IActionResult Create(Article article)
  36.         {
  37.             _articleRepository.AddNewArticle(article);
  38.             return View();
  39.         }
  40.         [HttpGet("/products/{idString}", Name = "IdString")]
  41.         public IActionResult Details(string idString)
  42.         {
  43.            
  44.             var article = _articleRepository.GetArticleByIdString(idString);
  45.             if (article == null)
  46.                 return RedirectToAction(nameof(Index));
  47.             return View(article);
  48.         }
  49.  
  50.         [HttpGet]
  51.         public IActionResult Edit(Article article)
  52.         {
  53.             return View();
  54.         }
  55.  
  56.         [HttpPost]
  57.         public IActionResult Edit(Article article, int id)
  58.         {
  59.             var art = _articleRepository.GetArticleById(id);
  60.             _articleRepository.EditArticle(art);
  61.             return View();
  62.         }
  63.  
  64.     }
  65. }
  66.  
  67. ////////Article Repository
  68.  
  69. using System;
  70. using System.Collections.Generic;
  71. using System.Linq;
  72. using System.Threading.Tasks;
  73.  
  74. namespace Apka1.Models
  75. {
  76.     public class ArticleRepository:IArticleRepository
  77.     {
  78.         private readonly AppDbContext _appDbContext;
  79.         public ArticleRepository(AppDbContext appDbContext)
  80.         {
  81.             _appDbContext = appDbContext;
  82.         }
  83.  
  84.         public IEnumerable<Article> GetAllArticles()
  85.         {
  86.             return _appDbContext.Articles;
  87.         }
  88.  
  89.         public Article GetArticleById(int artId)
  90.         {
  91.             return _appDbContext.Articles.FirstOrDefault(a => a.Id == artId);
  92.         }
  93.  
  94.         public void AddNewArticle(Article article)
  95.         {
  96.             _appDbContext.Add(article);
  97.             _appDbContext.SaveChanges();
  98.         }
  99.  
  100.         public Article GetArticleByIdString(string artIdString)
  101.         {
  102.             return _appDbContext.Articles.FirstOrDefault(a => a.IdString == artIdString);
  103.            
  104.         }
  105.  
  106.         public void EditArticle(Article article)
  107.         {
  108.             var art = _appDbContext.Articles.Find(article.Id);
  109.             art.Title =article.Title+"0k0k0k0";
  110.  
  111.             _appDbContext.Articles.Update(art);
  112.             _appDbContext.SaveChanges();
  113.  
  114.         }
  115.  
  116.     }
  117. }
  118.  
  119.  
  120. ///////////////Edit.cshtml
  121. @model Article
  122.  
  123. <h2>Edit</h2>
  124.  
  125. <form asp-controller="Home" asp-action="Edit" method="post">
  126.     <input type="hidden" asp-for="Id" />
  127.     <div>
  128.         <label asp-for="Title"></label>
  129.         <input asp-for="Title" />
  130.     </div>
  131.     <div>
  132.         <label asp-for="Text"></label>
  133.         <textarea asp-for="Text"></textarea>
  134.     </div>
  135.     <div>
  136.         <label asp-for="Price"></label>
  137.         <input asp-for="Price" />
  138.     </div>
  139.     <div>
  140.         <label asp-for="Capacity"></label>
  141.         <input asp-for="Capacity" />
  142.     </div>
  143.     <div>
  144.         <label asp-for="RealCapacity"></label>
  145.         <input asp-for="RealCapacity" />
  146.     </div>
  147.     <div>
  148.         <label asp-for="IsQuickCharge"></label>
  149.         <input type="checkbox" asp-for="IsQuickCharge" />
  150.     </div>
  151.     <div>
  152.         <input type="submit" name="save" value="Save" />
  153.     </div>
  154.  
  155. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement