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
- {
- //UserStorage us = new UserStorage();
- public ActionResult Details(int id)
- {
- List<User> userList = us.GetUserList();
- User user = userList.Where(u => u.userID == id).Single();
- return View("Details", user);
- }
- public ActionResult Create()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Create(User u)
- {
- if (ModelState.IsValid)
- {
- us.AddUser(u);
- return List();
- }
- return View(u);
- }
- // GET: User
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult List()
- {
- return View("List", us.GetUserList());
- }
- public List<User> GetUserList()
- {
- List<User> userList;
- if(Session["userList"]==null)
- {
- userList = new List<User>();
- }
- else
- {
- userList = (List<User>)Session["userList"];
- }
- ////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;
- }
- private void SaveUserList(List<User> userList)
- {
- Session["userList"] = userList;
- }
- public void AddUser(User u)
- {
- List<User> userList = GetUserList();
- userList.Add(u);
- SaveUserList(userList);
- }
- ///* ----------------------------------------------------------- */
- //private class UserStorage
- //{
- // List<User> userList;
- // public UserStorage()
- // {
- // userList = new List<User>();
- // }
- // //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 z zakresu 1-100")];
- public int age { get; set; }
- }
- }
- List view
- @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>
- 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.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>
- @*@{
- string color = "black";
- if(Model.age<18)
- {
- color = "red";
- }
- }*@
- <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>
- DetailsVM
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using ps_03_start.Models;
- using ps_03_start.ViewModels.UserVM;
- namespace ps_03_start.Views.User
- {
- 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 "black";
- }
- }
- }
- }
- 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>
- @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>@Html.ValidationMessageFor(m => m.age)</td>
- </tr>
- </table>
- <input type="submit" value="Zatwierdź" />
- }
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement