Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UserController
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using ps_03_start.Models;
- using ps_03_start.ViewModels.UserVM;
- namespace ps_03_start.Controllers
- {
- public class UserController : Controller
- {
- public ActionResult Details(int id)
- {
- List<User> userList = GetUserList();
- User user = userList.Where(u => u.userID == id).Single();
- DetailsVM details = new DetailsVM();
- details.user = user;
- return View("Details", details);
- }
- public ActionResult Create()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Create(User u)
- {
- if (ModelState.IsValid)
- {
- AddUser(u);
- return List();
- }
- return View(u);
- }
- // GET: User
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult List()
- {
- return View("List", GetUserList());
- }
- private List<User> GetUserList()
- {
- List<User> userList;
- if(Session["userList"] == null)
- {
- userList = new List<User>;
- }
- else
- {
- userList = (List<User>)Session["userList"];
- }
- return userList;
- }
- private void SaveUserList(List<User> userList)
- {
- Session["userList"] = userList;
- }
- public void AddUser(User u)
- {
- List<User> userList = GetUserList();
- userList.Add(u);
- SaveUserList(userList);
- }
- /* ----------------------------------------------------------- */
- /* Przenieśliśmy do góry
- private class UserStorage
- {
- List<User> userList;
- public UserStorage()
- {
- userList = new List<User>();
- }
- /*
- public List<User> GetUserList()
- {
- userList.Add(new User() { userID = 1211, firstName = "Acki", lastName = "Ackowski", age = 25 });
- userList.Add(new User() { userID = 2211, firstName = "Backi", lastName = "Backowski", age = 15 });
- userList.Add(new User() { userID = 3211, firstName = "Cacki", lastName = "Cackowski", age = 21 });
- return userList;
- }
- public void AddUser(User u)
- {
- userList.Add(u);
- }
- }
- */
- /* ----------------------------------------------------------- */
- }
- }
- User:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- namespace ps_03_start.Models
- {
- public class User
- {
- [Display(Name ="ID")]
- public int userID { get; set; }
- [Display(Name ="Imię")]
- public string firstName { get; set; }
- [Display(Name ="Nazwisko")]
- public string lastName { get; set; }
- [Display(Name ="Wiek")]
- [Range(1,100, ErrorMessage ="Wiek może być z zakresu 1-100")]
- public int age { get; set; }
- }
- }
- DetailsVM: ( Utworzony nowy folder ViewModels a w nim kolejny UserVM i tu powinien być ten plik )
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using ps_03_start.Models;
- namespace ps_03_start.ViewModels.UserVM
- {
- public class DetailsVM
- {
- public User user { get; set; }
- public string fullName
- {
- get
- {
- return user.lastName + " " + user.firstName;
- }
- }
- public string ageColor
- {
- get
- {
- if (user.age < 18)
- {
- return "red";
- }
- return "green";
- }
- }
- }
- }
- Create
- @model ps_03_start.Models.User
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Create</title>
- </head>
- <body>
- <div style="color:red"> @Html.ValidationSummary()</div>
- <div>
- @using (Html.BeginForm())
- {
- <table>
- <tr>
- <th style="text-align: right">@Html.DisplayNameFor(m => m.userID)</th>
- <td>@Html.EditorFor(m => m.userID)</td>
- </tr>
- <tr>
- <th style="text-align: right">@Html.DisplayNameFor(m => m.firstName)</th>
- <td>@Html.EditorFor(m => m.firstName)</td>
- </tr>
- <tr>
- <th style="text-align: right">@Html.DisplayNameFor(m => m.lastName)</th>
- <td>@Html.EditorFor(m => m.lastName)</td>
- </tr>
- <tr>
- <th style="text-align: right">@Html.DisplayNameFor(m => m.age)</th>
- <td>@Html.EditorFor(m => m.age)</td>
- <td style="color:red">@Html.ValidationMessageFor(m => m.age)</td>
- </tr>
- </table>
- <input type="submit" value="Zatwierdź" />
- }
- </div>
- </body>
- </html>
- Details
- @model ps_03_start.ViewModels.UserVM.DetailsVM
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Details</title>
- </head>
- <body>
- <div>
- <table>
- <tr>
- <th style="text-align:right">@Html.DisplayNameFor(m=>m.user.userID): </th>
- <td >@Html.DisplayFor(m => Model.user.userID) </td>
- </tr>
- <tr>
- <th style="text-align:right">Nazwisko i Imię: </th>
- <td>@Html.DisplayFor(m => Model.fullName)</td>
- </tr>
- <tr>
- <th style="text-align:right">@Html.DisplayNameFor(m => m.user.age): </th>
- <td style="color:@Model.ageColor">@Html.DisplayFor(m => Model.user.age)</td>
- </tr>
- </table>
- </div>
- </body>
- </html>
- List
- @model IEnumerable<ps_03_start.Models.User>
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>List</title>
- </head>
- <body>
- <div>
- <p>
- @Html.ActionLink("DODAJ", "Create")
- </p>
- <table>
- @foreach (var u in Model)
- {
- <tr>
- <td>@Html.DisplayFor(m => u.userID)</td>
- <td>@Html.DisplayFor(m => u.lastName) @Html.DisplayFor(m => u.firstName)</td>
- <td>@Html.DisplayFor(m => u.age)</td>
- </tr>
- }
- </table>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement