Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. public class Customer
  2. {
  3. [Key]
  4. public int Id { get; set; }
  5.  
  6. [Required]
  7. [DisplayName("Имя")]
  8. public string Name { get; set; }
  9.  
  10. [Required]
  11. [DisplayName("Адрес")]
  12. public IList<Address> Addresses
  13. {
  14. get
  15. {
  16. return this._address;
  17. }
  18.  
  19. set => this._address = value;
  20. }
  21.  
  22. private IList<Address> _address = new List<Address>();
  23.  
  24. [Required]
  25. [DisplayName("Телефон")]
  26. public IList<Phone> Phones
  27. {
  28. get
  29. {
  30. return this._phones;
  31. }
  32.  
  33. set => this._phones = value;
  34. }
  35.  
  36. private IList<Phone> _phones = new List<Phone>();
  37.  
  38. [Required]
  39. [DisplayName("E-mail")]
  40. public IList<Email> Emails
  41. {
  42. get
  43. {
  44. return this._email;
  45. }
  46.  
  47. set => this._email = value;
  48. }
  49.  
  50. private IList<Email> _email = new List<Email>();
  51.  
  52. [DisplayName("Обновлен")]
  53. public IList<ProfileUpdate> ProfileUpdates { get; set; }
  54.  
  55. [DisplayName("Заметка")]
  56. public string Notes { get; set; }
  57.  
  58. [DisplayName("Аккаунт")]
  59. public bool Active { get; set; } = true; // по умолчанию аккаунт "Действующий"
  60. }
  61. public class Email
  62. {
  63. public enum emailTypes
  64. {
  65. Work,
  66. Home
  67. }
  68.  
  69. [Key]
  70. public int Id { get; set; }
  71.  
  72. [Required]
  73. public emailTypes Type { get; set; }
  74.  
  75. [Required]
  76. [EmailAddress]
  77. [DisplayName("E-mail")]
  78. public string Value { get; set; }
  79.  
  80. public IEnumerable<SelectListItem> EmailTypeSelectListItems
  81. {
  82. get
  83. {
  84. foreach (emailTypes type in Enum.GetValues(typeof(emailTypes)))
  85. {
  86. var selectListItem = new SelectListItem
  87. {
  88. Text = type.ToString(),
  89. Value = type.ToString(),
  90. Selected = this.Type == type
  91. };
  92. yield return selectListItem;
  93. }
  94. }
  95. }
  96. }
  97. public class Phone
  98. {
  99. public enum phoneTypes
  100. {
  101. Work,
  102. Mobile
  103. }
  104.  
  105. [Key]
  106. public int Id { get; set; }
  107.  
  108. [Required]
  109. public phoneTypes Type { get; set; }
  110.  
  111. [Required]
  112. [Phone]
  113. [DisplayName("Телефон")]
  114. public string Value { get; set; }
  115.  
  116. public IEnumerable<SelectListItem> PhoneTypeSelectListItems
  117. {
  118. get
  119. {
  120. foreach (phoneTypes type in Enum.GetValues(typeof(phoneTypes)))
  121. {
  122. var selectListItem = new SelectListItem
  123. {
  124. Text = type.ToString(),
  125. Value = type.ToString(),
  126. Selected = this.Type == type
  127. };
  128. yield return selectListItem;
  129. }
  130. }
  131. }
  132. }
  133. public class ProfileUpdate
  134. {
  135. [Key]
  136. public int Id { get; set; }
  137.  
  138. public DateTime UpdateDateTime { get; set; }
  139. }
  140. public class Address
  141. {
  142. public enum addressTypes
  143. {
  144. Visiting,
  145. Home
  146. }
  147.  
  148. [Key]
  149. public int Id { get; set; }
  150.  
  151. [Required]
  152. public addressTypes Type { get; set; }
  153.  
  154. [Required]
  155. [DisplayName("Адрес 1")]
  156. public string Street1 { get; set; }
  157.  
  158. [DisplayName("Адрес 2")]
  159. public string Street2 { get; set; }
  160.  
  161. [Required]
  162. [DisplayName("Почтовый индекс")]
  163. public int PostalCode { get; set; }
  164.  
  165. [Required]
  166. [DisplayName("Город")]
  167. public string Town { get; set; }
  168.  
  169. public IEnumerable<SelectListItem> AddressTypeSelectListItems
  170. {
  171. get
  172. {
  173. foreach (addressTypes type in Enum.GetValues(typeof(addressTypes)))
  174. {
  175. var selectListItem = new SelectListItem
  176. {
  177. Text = type.ToString(),
  178. Value = type.ToString(),
  179. Selected = this.Type == type
  180. };
  181. yield return selectListItem;
  182. }
  183. }
  184. }
  185. }
  186.  
  187. <div class="form-horizontal">
  188. <hr />
  189. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  190. @Html.HiddenFor(model => model.Id)
  191. @Html.HiddenFor(model => model.Active, true)
  192. <div class="form-group">
  193. @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
  194. <div class="col-md-10">
  195. @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
  196. @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
  197. </div>
  198. </div>
  199.  
  200. <div class="form-group">
  201. @Html.LabelFor(model => model.Notes, new { @class = "control-label col-md-2" })
  202. <div class="col-md-10">
  203. @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
  204. @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
  205. </div>
  206. </div>
  207.  
  208. <div class="form-group">
  209. @Html.Label("Телефон", new { @class = "control-label col-md-2" })
  210. <div class="col-md-10">
  211. <div id="phoneList">
  212. @for (var i = 0; i < Model.Phones.Count; i++)
  213. {
  214. <div class="phoneRow">
  215. @Html.DropDownList("Phones[" + i + "].Type", Model.Phones[i].PhoneTypeSelectListItems, new { @class = "phoneType form-control" })
  216. @Html.TextBoxFor(model => Model.Phones[i].Value, new { placeholder = "Номер телефона", @class = "phoneNumber form-control" })
  217. <a href="javascript:void(0);" class="remRowPhone"><span class="glyphicon glyphicon glyphicon-trash"></span>Удалить</a>
  218. </div>
  219. }
  220. </div>
  221. <p>
  222. <a href="javascript:void(0);" class="addRowPhone"><span class="glyphicon glyphicon-plus"></span>Добавить номер</a>
  223. </p>
  224. @Html.ValidationMessageFor(model => model.Phones, "", new { @class = "text-danger" })
  225. </div>
  226. </div>
  227.  
  228. <div class="form-group">
  229. @Html.Label("Email", new { @class = "control-label col-md-2" })
  230. <div class="col-md-10">
  231. <div id="emailList">
  232. @for (var i = 0; i < Model.Emails.Count; i++)
  233. {
  234. <div class="emailRow">
  235. @Html.DropDownList("Emails[" + i + "].Type", Model.Emails[i].EmailTypeSelectListItems, new { @class = "emailType form-control" })
  236. @Html.TextBoxFor(model => Model.Emails[i].Value, new { placeholder = "Электропочта", @class = "emailValue form-control" })
  237. <a href="javascript:void(0);" class="remRowEmail"><span class="glyphicon glyphicon glyphicon-trash"></span>Удалить</a>
  238. </div>
  239. }
  240. </div>
  241. <p>
  242. <a href="javascript:void(0);" class="addRowEmail"><span class="glyphicon glyphicon-plus"></span>Добавить Email</a>
  243. </p>
  244. @Html.ValidationMessageFor(model => model.Emails, "", new { @class = "text-danger" })
  245. </div>
  246. </div>
  247.  
  248. <div class="form-group">
  249. @Html.Label("Адрес", new { @class = "control-label col-md-2" })
  250. <div class="col-md-10">
  251. <div id="addressList">
  252. @for (var i = 0; i < Model.Addresses.Count; i++)
  253. {
  254. <div class="addressRow">
  255. @Html.DropDownList("Adresses[" + i + "].Type", Model.Addresses[i].AddressTypeSelectListItems, new { @class = "addressType form-control" })
  256. @Html.TextBoxFor(model => Model.Addresses[i].PostalCode, new { placeholder = "Почтовый индекс", @class = "addressPostalCode form-control" })
  257. @Html.TextBoxFor(model => Model.Addresses[i].Town, new { placeholder = "Город", @class = "addressTown form-control" })
  258. @Html.TextBoxFor(model => Model.Addresses[i].Street1, new { placeholder = "Улица", @class = "addressStreet1 form-control" })
  259. @Html.TextBoxFor(model => Model.Addresses[i].Street2, new { placeholder = "Номер дома или квартиры", @class = "addressStreet2 form-control" })
  260. <a href="javascript:void(0);" class="remRowAddress"><span class="glyphicon glyphicon glyphicon-trash"></span>Удалить</a>
  261. </div>
  262. }
  263. </div>
  264. <p>
  265. <a href="javascript:void(0);" class="addRowAddress"><span class="glyphicon glyphicon-plus"></span>Добавить Адрес</a>
  266. </p>
  267. @Html.ValidationMessageFor(model => model.Addresses, "", new { @class = "text-danger" })
  268. </div>
  269. </div>
  270.  
  271. <div class="form-group">
  272. <div class="col-md-offset-2 col-md-10">
  273. <input type="submit" value="Сохранить" class="btn btn-default" />
  274. </div>
  275. </div>
  276. </div>
  277.  
  278. [HttpPost]
  279. [ValidateAntiForgeryToken]
  280. public async Task<ActionResult> Edit(Customer customer)
  281. {
  282. if (!this.ModelState.IsValid)
  283. {
  284. return this.View(customer);
  285. }
  286.  
  287. this.db.Entry(customer).State = EntityState.Modified;
  288. await this.db.SaveChangesAsync().ConfigureAwait(false);
  289. return this.RedirectToAction("Index");
  290. }
Add Comment
Please, Sign In to add comment