Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. @model WebApplication1.Controllers.PersonViewModel
  2. @{
  3. ViewBag.Title = "Index";
  4. }
  5.  
  6. <h1>@ViewBag.Title</h1>
  7.  
  8. @using(Html.BeginForm())
  9. {
  10. @Html.LabelFor(x => x.FirstName)
  11. @Html.TextBoxFor(x => x.FirstName, new { @class = "form-control" })
  12.  
  13. @Html.LabelFor(x => x.LastName)
  14. @Html.PasswordFor(x => x.LastName, new { @class = "form-control" })
  15.  
  16. @Html.LabelFor(x => x.Address.City)
  17. @Html.TextBoxFor(x => x.Address.City, new { @class = "form-control" })
  18.  
  19. @Html.LabelFor(x => x.Address.Number)
  20. @Html.TextBoxFor(x => x.Address.Number, new { @class = "form-control" })
  21.  
  22. @Html.LabelFor(x => x.Address.StateId)
  23. @Html.DropDownListFor(x => x.Address.StateId,
  24. Model.States?.Select(x => new SelectListItem
  25. { Text = x.Name, Value = x.Id.ToString() }), "--Wybierz--",
  26. new { @class = "form-control" })
  27.  
  28. <button class="btn btn-primary">Klik</button>
  29. }
  30.  
  31.  
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Linq;
  35. using System.Web;
  36. using System.Web.Mvc;
  37.  
  38. namespace WebApplication1.Controllers
  39. {
  40. public class HomeController : Controller
  41. {
  42.  
  43. public ActionResult Index()
  44. {
  45. var model = new PersonViewModel
  46. {
  47. FirstName = "Jan",
  48. };
  49.  
  50. InternalIndex(model);
  51.  
  52. return View(model);
  53. }
  54.  
  55. [HttpPost]
  56. public ActionResult Index(PersonViewModel p)
  57. {
  58. InternalIndex(p);
  59.  
  60. return View(p);
  61. }
  62.  
  63. private PersonViewModel InternalIndex(PersonViewModel p)
  64. {
  65. p.States = new[]
  66. {
  67. new StateItem(1, "śląskie"),
  68. new StateItem(2, "mazowieckie"),
  69. new StateItem(3, "małopolskie"),
  70. new StateItem(4, "pomorskie"),
  71. new StateItem(5, "warmińsko-mazurskie")
  72. };
  73.  
  74. return p;
  75. }
  76. }
  77.  
  78. public class PersonViewModel
  79. {
  80. public string FirstName { get; set; }
  81. public string LastName { get; set; }
  82. public AddressViewModel Address { get; set; }
  83.  
  84. public IEnumerable<StateItem> States { get; set; }
  85. }
  86.  
  87. public class StateItem
  88. {
  89. public int Id { get; set; }
  90. public string Name { get; set; }
  91.  
  92. public StateItem()
  93. {
  94. }
  95.  
  96. public StateItem(int id, string name)
  97. {
  98. Id = id;
  99. Name = name;
  100. }
  101. }
  102.  
  103. public class AddressViewModel
  104. {
  105. public string City { get; set; }
  106. public int Number { get; set; }
  107. public int StateId { get; set; }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement