Advertisement
Guest User

Untitled

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