Практика по ресурсу Metanit
Модели
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Player { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string Position { get; set; } public int? TeamId { get; set; } public Team Team { get; set; } } public class Team { public int Id { get; set; } public string Name { get; set; } public string Coach { get; set; } public ICollection<Player> Players { get; set; } public Team() { Players = new List<Player>(); } } |
Модель для фильтрации
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; namespace NavigationProperty.Models { public class PlayersListViewModel { public IEnumerable<Player> Players { get; set; } public SelectList Teams { get; set; } public SelectList Positions { get; set; } } } |
Контроллер
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using modelsTraining2.Models; namespace modelsTraining2.Controllers { public class FilterPlayersController : Controller { private SoccerContext db = new SoccerContext(); // GET: FilterPlayers public ActionResult Index(int? team, string position) { IQueryable<Player> players = db.Players.Include(p => p.Team); if (team != null && team != 0) { players = players.Where(p => p.TeamId == team); } if (!String.IsNullOrEmpty(position) && !position.Equals("Все")) { players = players.Where(p => p.Position == position); } List<Team> teams = db.Teams.ToList(); // устанавливаем начальный элемент, который позволит выбрать всех teams.Insert(0, new Team { Name = "Все", Id = 0 }); PlayersListViewModel plvm = new PlayersListViewModel { Players = players.ToList(), Teams = new SelectList(teams, "Id", "Name"), Positions = new SelectList(new List<string>() { "Все", "Нападающий", "Полузащитник", "Защитник", "Вратарь" }) }; return View(plvm); } // GET: FilterPlayers/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Player player = db.Players.Find(id); if (player == null) { return HttpNotFound(); } return View(player); } // GET: FilterPlayers/Create public ActionResult Create() { ViewBag.TeamId = new SelectList(db.Teams, "Id", "Name"); return View(); } // POST: FilterPlayers/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Name,Age,Position,TeamId")] Player player) { if (ModelState.IsValid) { db.Players.Add(player); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.TeamId = new SelectList(db.Teams, "Id", "Name", player.TeamId); return View(player); } // GET: FilterPlayers/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Player player = db.Players.Find(id); if (player == null) { return HttpNotFound(); } ViewBag.TeamId = new SelectList(db.Teams, "Id", "Name", player.TeamId); return View(player); } // POST: FilterPlayers/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "Id,Name,Age,Position,TeamId")] Player player) { if (ModelState.IsValid) { db.Entry(player).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.TeamId = new SelectList(db.Teams, "Id", "Name", player.TeamId); return View(player); } // GET: FilterPlayers/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Player player = db.Players.Find(id); if (player == null) { return HttpNotFound(); } return View(player); } // POST: FilterPlayers/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Player player = db.Players.Find(id); db.Players.Remove(player); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } } |
Представление для фильтрации
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
@model modelsTraining2.Models.IndexViewModel @using modelsTraining2.Models @{ ViewBag.Title = "Home Page"; } <table class="table"> <tr> <td>Модель</td> <td>Производитель</td> </tr> @foreach (var item in Model.Phones) { <tr> <td>@item.Model</td> <td>@item.Producer</td> </tr> } </table> <br /> <div class="btn-group"> @Html.PageLinks(Model.PageInfo, x => Url.Action("Index", new { page = x })) </div> |
Результат