Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Catalog
- {
- public int id { get; set; }
- public int pid { get; set; }
- public string title { get; set; }
- }
- public ViewResult Create()
- {
- List<SelectListItem> list = new List<SelectListItem>();
- list.Add(new SelectListItem() { Value = "0", Text = "Без родителя" });
- foreach (var p in repository.Catalogs)
- {
- list.Add(new SelectListItem() { Value = p.id.ToString(), Text = p.title });
- }
- ViewBag.DropDownValues = new SelectList(list, "Value", "Text");
- ViewBag.FormTitle = null;
- return View();
- }
- [HttpPost]
- public ActionResult Create(Catalog catalog)
- {
- if (ModelState.IsValid)
- {
- repository.SaveCatalog(catalog);
- TempData["message"] = string.Format("Новая категория "{0}" была сохранены", catalog.title);
- return RedirectToAction("Index");
- }
- else
- {
- return View(catalog);
- }
- }
- public void SaveCatalog(Catalog catalog)
- {
- if (catalog.id == 0)
- context.Catalogs.Add(catalog);
- else
- {
- Catalog dbEntry = context.Catalogs.Find(catalog.id);
- if (dbEntry != null)
- {
- dbEntry.pid = catalog.pid;
- dbEntry.title = catalog.title;
- }
- }
- context.SaveChanges();
- }
- @model EStore.Domain.Entities.Catalog
- @{
- ViewBag.TitlePage = "Админ панель: добавление категории";
- Layout = "~/Views/Shared/_AdminLayout.cshtml";
- }
- <div class="panel">
- <div class="panel-heading">
- <h3>Создание категории</h3>
- </div>
- @using (Html.BeginForm("Create", "Addcatalog"))
- {
- <div class="panel-body">
- <div class="form-group">
- @Html.HiddenFor(m => m.id)
- @Html.Label("Родительская категория: ", new { @class = "col-md-2 control-label" })
- @Html.DropDownList("pid", ViewBag.DropDownValues as SelectList, new { @class = "form-control" })
- @Html.Label("Название категории: ", new { @class = "col-md-2 control-label" })
- @Html.TextBox("title", null, new { @class = "form-control" })
- </div>
- </div>
- <div class="panel-footer">
- <input type="submit" value="Сохранить" class="btn btn-primary" />
- @Html.ActionLink("вернуться к списку", "Index", null, new
- {
- @class = "btn btn-default"
- })
- </div>
- }
- </div>
- 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