Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. UserController
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using ps_03_start.Models;
  9. using ps_03_start.ViewModels.UserVM;
  10.  
  11. namespace ps_03_start.Controllers
  12. {
  13. public class UserController : Controller
  14. {
  15.  
  16.  
  17.  
  18.  
  19. public ActionResult Details(int id)
  20. {
  21. List<User> userList = GetUserList();
  22. User user = userList.Where(u => u.userID == id).Single();
  23.  
  24. DetailsVM details = new DetailsVM();
  25. details.user = user;
  26.  
  27. return View("Details", details);
  28. }
  29.  
  30. public ActionResult Create()
  31. {
  32.  
  33. return View();
  34. }
  35.  
  36.  
  37. [HttpPost]
  38. public ActionResult Create(User u)
  39. {
  40. if (ModelState.IsValid)
  41. {
  42. AddUser(u);
  43. return List();
  44. }
  45. return View(u);
  46. }
  47.  
  48.  
  49.  
  50. // GET: User
  51. public ActionResult Index()
  52. {
  53. return View();
  54. }
  55.  
  56.  
  57. public ActionResult List()
  58. {
  59.  
  60. return View("List", GetUserList());
  61. }
  62.  
  63. private List<User> GetUserList()
  64. {
  65. List<User> userList;
  66. if(Session["userList"] == null)
  67. {
  68. userList = new List<User>;
  69. }
  70. else
  71. {
  72. userList = (List<User>)Session["userList"];
  73. }
  74. return userList;
  75. }
  76.  
  77. private void SaveUserList(List<User> userList)
  78. {
  79. Session["userList"] = userList;
  80. }
  81.  
  82. public void AddUser(User u)
  83. {
  84. List<User> userList = GetUserList();
  85. userList.Add(u);
  86. SaveUserList(userList);
  87. }
  88.  
  89.  
  90.  
  91. /* ----------------------------------------------------------- */
  92. /* Przenieśliśmy do góry
  93. private class UserStorage
  94. {
  95. List<User> userList;
  96. public UserStorage()
  97. {
  98. userList = new List<User>();
  99. }
  100.  
  101. /*
  102. public List<User> GetUserList()
  103. {
  104. userList.Add(new User() { userID = 1211, firstName = "Acki", lastName = "Ackowski", age = 25 });
  105. userList.Add(new User() { userID = 2211, firstName = "Backi", lastName = "Backowski", age = 15 });
  106. userList.Add(new User() { userID = 3211, firstName = "Cacki", lastName = "Cackowski", age = 21 });
  107.  
  108. return userList;
  109. }
  110.  
  111.  
  112. public void AddUser(User u)
  113. {
  114. userList.Add(u);
  115. }
  116.  
  117. }
  118. */
  119.  
  120. /* ----------------------------------------------------------- */
  121.  
  122.  
  123.  
  124. }
  125.  
  126. }
  127.  
  128.  
  129. User:
  130.  
  131.  
  132. using System;
  133. using System.Collections.Generic;
  134. using System.Linq;
  135. using System.Web;
  136. using System.ComponentModel.DataAnnotations;
  137.  
  138. namespace ps_03_start.Models
  139. {
  140. public class User
  141. {
  142. [Display(Name ="ID")]
  143. public int userID { get; set; }
  144.  
  145. [Display(Name ="Imię")]
  146. public string firstName { get; set; }
  147.  
  148. [Display(Name ="Nazwisko")]
  149. public string lastName { get; set; }
  150.  
  151. [Display(Name ="Wiek")]
  152. [Range(1,100, ErrorMessage ="Wiek może być z zakresu 1-100")]
  153. public int age { get; set; }
  154.  
  155.  
  156. }
  157. }
  158.  
  159. DetailsVM: ( Utworzony nowy folder ViewModels a w nim kolejny UserVM i tu powinien być ten plik )
  160.  
  161. using System;
  162. using System.Collections.Generic;
  163. using System.Linq;
  164. using System.Web;
  165. using ps_03_start.Models;
  166.  
  167. namespace ps_03_start.ViewModels.UserVM
  168. {
  169. public class DetailsVM
  170. {
  171.  
  172. public User user { get; set; }
  173.  
  174. public string fullName
  175. {
  176. get
  177. {
  178. return user.lastName + " " + user.firstName;
  179. }
  180. }
  181.  
  182. public string ageColor
  183. {
  184. get
  185. {
  186. if (user.age < 18)
  187. {
  188. return "red";
  189. }
  190.  
  191. return "green";
  192. }
  193. }
  194. }
  195. }
  196.  
  197.  
  198. Create
  199.  
  200.  
  201. @model ps_03_start.Models.User
  202. @{
  203. Layout = null;
  204. }
  205.  
  206. <!DOCTYPE html>
  207.  
  208. <html>
  209. <head>
  210. <meta name="viewport" content="width=device-width" />
  211. <title>Create</title>
  212. </head>
  213. <body>
  214. <div style="color:red"> @Html.ValidationSummary()</div>
  215. <div>
  216. @using (Html.BeginForm())
  217. {
  218. <table>
  219. <tr>
  220. <th style="text-align: right">@Html.DisplayNameFor(m => m.userID)</th>
  221. <td>@Html.EditorFor(m => m.userID)</td>
  222. </tr>
  223. <tr>
  224. <th style="text-align: right">@Html.DisplayNameFor(m => m.firstName)</th>
  225. <td>@Html.EditorFor(m => m.firstName)</td>
  226. </tr>
  227.  
  228. <tr>
  229. <th style="text-align: right">@Html.DisplayNameFor(m => m.lastName)</th>
  230. <td>@Html.EditorFor(m => m.lastName)</td>
  231. </tr>
  232.  
  233. <tr>
  234. <th style="text-align: right">@Html.DisplayNameFor(m => m.age)</th>
  235. <td>@Html.EditorFor(m => m.age)</td>
  236. <td style="color:red">@Html.ValidationMessageFor(m => m.age)</td>
  237. </tr>
  238. </table>
  239. <input type="submit" value="Zatwierdź" />
  240. }
  241. </div>
  242. </body>
  243. </html>
  244.  
  245.  
  246. Details
  247.  
  248.  
  249. @model ps_03_start.ViewModels.UserVM.DetailsVM
  250. @{
  251. Layout = null;
  252. }
  253.  
  254. <!DOCTYPE html>
  255.  
  256. <html>
  257. <head>
  258. <meta name="viewport" content="width=device-width" />
  259. <title>Details</title>
  260. </head>
  261. <body>
  262. <div>
  263. <table>
  264. <tr>
  265. <th style="text-align:right">@Html.DisplayNameFor(m=>m.user.userID): </th>
  266. <td >@Html.DisplayFor(m => Model.user.userID) </td>
  267. </tr>
  268. <tr>
  269. <th style="text-align:right">Nazwisko i Imię: </th>
  270. <td>@Html.DisplayFor(m => Model.fullName)</td>
  271. </tr>
  272. <tr>
  273. <th style="text-align:right">@Html.DisplayNameFor(m => m.user.age): </th>
  274. <td style="color:@Model.ageColor">@Html.DisplayFor(m => Model.user.age)</td>
  275. </tr>
  276. </table>
  277. </div>
  278. </body>
  279. </html>
  280.  
  281.  
  282. List
  283.  
  284.  
  285. @model IEnumerable<ps_03_start.Models.User>
  286.  
  287. @{
  288. Layout = null;
  289. }
  290.  
  291. <!DOCTYPE html>
  292.  
  293. <html>
  294. <head>
  295. <meta name="viewport" content="width=device-width" />
  296. <title>List</title>
  297. </head>
  298. <body>
  299. <div>
  300. <p>
  301. @Html.ActionLink("DODAJ", "Create")
  302. </p>
  303. <table>
  304. @foreach (var u in Model)
  305. {
  306. <tr>
  307. <td>@Html.DisplayFor(m => u.userID)</td>
  308. <td>@Html.DisplayFor(m => u.lastName) @Html.DisplayFor(m => u.firstName)</td>
  309. <td>@Html.DisplayFor(m => u.age)</td>
  310. </tr>
  311. }
  312. </table>
  313. </div>
  314. </body>
  315. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement