Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. public class Catalog
  2. {
  3. public int id { get; set; }
  4. public int pid { get; set; }
  5. public string title { get; set; }
  6.  
  7. }
  8.  
  9. public ViewResult Create()
  10. {
  11.  
  12. List<SelectListItem> list = new List<SelectListItem>();
  13. list.Add(new SelectListItem() { Value = "0", Text = "Без родителя" });
  14. foreach (var p in repository.Catalogs)
  15. {
  16. list.Add(new SelectListItem() { Value = p.id.ToString(), Text = p.title });
  17. }
  18. ViewBag.DropDownValues = new SelectList(list, "Value", "Text");
  19. ViewBag.FormTitle = null;
  20. return View();
  21. }
  22. [HttpPost]
  23. public ActionResult Create(Catalog catalog)
  24. {
  25. if (ModelState.IsValid)
  26. {
  27. repository.SaveCatalog(catalog);
  28. TempData["message"] = string.Format("Новая категория "{0}" была сохранены", catalog.title);
  29. return RedirectToAction("Index");
  30. }
  31. else
  32. {
  33. return View(catalog);
  34. }
  35. }
  36.  
  37. public void SaveCatalog(Catalog catalog)
  38. {
  39. if (catalog.id == 0)
  40. context.Catalogs.Add(catalog);
  41. else
  42. {
  43. Catalog dbEntry = context.Catalogs.Find(catalog.id);
  44. if (dbEntry != null)
  45. {
  46. dbEntry.pid = catalog.pid;
  47. dbEntry.title = catalog.title;
  48. }
  49. }
  50. context.SaveChanges();
  51. }
  52.  
  53. @model EStore.Domain.Entities.Catalog
  54.  
  55. @{
  56. ViewBag.TitlePage = "Админ панель: добавление категории";
  57. Layout = "~/Views/Shared/_AdminLayout.cshtml";
  58. }
  59.  
  60. <div class="panel">
  61. <div class="panel-heading">
  62. <h3>Создание категории</h3>
  63. </div>
  64.  
  65. @using (Html.BeginForm("Create", "Addcatalog"))
  66. {
  67. <div class="panel-body">
  68. <div class="form-group">
  69. @Html.HiddenFor(m => m.id)
  70. @Html.Label("Родительская категория: ", new { @class = "col-md-2 control-label" })
  71. @Html.DropDownList("pid", ViewBag.DropDownValues as SelectList, new { @class = "form-control" })
  72. @Html.Label("Название категории: ", new { @class = "col-md-2 control-label" })
  73. @Html.TextBox("title", null, new { @class = "form-control" })
  74. </div>
  75.  
  76. </div>
  77. <div class="panel-footer">
  78. <input type="submit" value="Сохранить" class="btn btn-primary" />
  79. @Html.ActionLink("вернуться к списку", "Index", null, new
  80. {
  81. @class = "btn btn-default"
  82. })
  83. </div>
  84. }
  85. </div>
  86.  
  87. The ViewData item that has the key 'pid' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement