Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. // GET: Model/Edit/5
  2. public ActionResult Edit(int? id)
  3. {
  4. if (id == null)
  5. {
  6. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  7. }
  8. Model model = db.Models.Find(id);
  9. if (model == null)
  10. {
  11. return HttpNotFound();
  12. }
  13.  
  14. ViewBag.Manufacturers = GetManufacturerList(model);
  15.  
  16. return View(model);
  17. }
  18.  
  19. // POST: Model/Edit/5
  20. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  21. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  22. [HttpPost]
  23. [ValidateAntiForgeryToken]
  24. public ActionResult EditPost(int? id)
  25. {
  26. if (id == null)
  27. {
  28. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  29. }
  30. var modelToUpdate = db.Models.Find(id);
  31. if (TryUpdateModel(modelToUpdate, "",
  32. new string[] { "ModelName", "ManufacturerID" }))
  33. {
  34. try
  35. {
  36. db.SaveChanges();
  37.  
  38. return RedirectToAction("Index");
  39. }
  40. catch (DataException /* dex */)
  41. {
  42. //Log the error (uncomment dex variable name and add a line here to write a log.
  43. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
  44. }
  45. }
  46.  
  47. ViewBag.Manufacturers = GetManufacturerList();
  48.  
  49. return View(modelToUpdate);
  50. }
  51.  
  52. @using (Html.BeginForm())
  53. {
  54. @Html.AntiForgeryToken()
  55.  
  56. <div class="form-horizontal">
  57. <h4>Model</h4>
  58. <hr />
  59. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  60. @Html.HiddenFor(model => model.ModelID)
  61.  
  62. <div class="form-group">
  63. @Html.LabelFor(model => model.ModelName, htmlAttributes: new { @class = "control-label col-md-2" })
  64. <div class="col-md-10">
  65. @Html.EditorFor(model => model.ModelName, new { htmlAttributes = new { @class = "form-control" } })
  66. @Html.ValidationMessageFor(model => model.ModelName, "", new { @class = "text-danger" })
  67. </div>
  68. </div>
  69.  
  70. <div class="form-group">
  71. @Html.LabelFor(model => model.Manufacturer.ManufacturerName, "Manufacturer",
  72. htmlAttributes: new { @class = "control-label col-md-2" })
  73. <div class="col-md-5">
  74. @Html.DropDownList("ManufacturerID", (List<SelectListItem>)ViewBag.Manufacturers,
  75. htmlAttributes: new { @class = "form-control" })
  76. @Html.ValidationMessageFor(model => model.Manufacturer.ManufacturerName, "",
  77. new { @class = "text-danger" })
  78. </div>
  79. </div>
  80.  
  81. <div class="form-group">
  82. <div class="col-md-offset-2 col-md-10">
  83. <input type="submit" value="Save" class="btn btn-default" />
  84. </div>
  85. </div>
  86. </div>
  87. }
  88.  
  89. public class Model
  90. {
  91. [Required]
  92. [Display(Name = "Manufacturer")]
  93. [ForeignKey("Manufacturer")]
  94. public int ManufacturerID { get; set; }
  95.  
  96. [Required]
  97. public int ModelID { get; set; }
  98.  
  99. [Required]
  100. [StringLength(50, ErrorMessage = "Model cannot be longer than 50 characters.")]
  101. [RegularExpression(@"^[a-zA-Z0-9.-/() ]+$", ErrorMessage = "Invalid characters used. A-Z or a-z, 0-9, '.', '-', '()' and '/' allowed.")]
  102. [Display(Name = "Model")]
  103. public string ModelName { get; set; }
  104.  
  105. public virtual Manufacturer Manufacturer { get; set; }
  106. public virtual ICollection<Item> Items { get; set; }
  107. }
  108.  
  109. public class Manufacturer
  110. {
  111. [Required]
  112. public int ManufacturerID { get; set; }
  113.  
  114. [Required]
  115. [StringLength(50, ErrorMessage = "Manufacturer cannot be longer than 50 characters.")]
  116. [RegularExpression(@"^[a-zA-Z0-9.-/() ]+$", ErrorMessage = "Invalid characters used. A-Z or a-z, 0-9, '.', '-', '()' and '/' allowed.")]
  117. [Display(Name = "Manufacturer")]
  118. public string ManufacturerName { get; set; }
  119.  
  120. public virtual ICollection<Model> Models { get; set; }
  121. public virtual ICollection<Item> Items { get; set; }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement