Guest User

Untitled

a guest
Apr 22nd, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. public class ProductCreateViewModel
  2. {
  3. public Product Product { get; set; }
  4. public ICollection<IFormFile> Images { get; set; }
  5. }
  6.  
  7. [HttpPost]
  8. [ValidateAntiForgeryToken]
  9. public async Task<IActionResult> Create([Bind("Product.Id,Product.CategoryId,Product.Description,Product.Title")] ProductCreateViewModel productVM)
  10. {
  11. if (ModelState.IsValid)
  12. {
  13. _context.Add(productVM.Product);
  14. await _context.SaveChangesAsync();
  15. return RedirectToAction("Index");
  16. }
  17. ViewData["CategoryId"] = new SelectList(_context.Categories.Include(c => c.Categories).Where(c => c.ParentCategoryId == null), "Id", "Name", productVM.Product.CategoryId);
  18. return View(productVM);
  19. }
  20.  
  21. @model CatalogWebApp.Models.ProductsViewModels.ProductCreateViewModel
  22.  
  23. @{
  24. ViewData["Title"] = "Add Product";
  25. ViewData["BigPageTitle"] = "Products";
  26. ViewData["PageBoxTitle"] = "Add New Product";
  27. }
  28.  
  29. <form asp-action="Create">
  30. <div class="form-horizontal">
  31. <div asp-validation-summary="ModelOnly" class="text-danger"></div>
  32. <div class="form-group">
  33. <label asp-for="Product.CategoryId" class="col-md-2 control-label"></label>
  34. <div class="col-md-10">
  35. <select name="Product.CategoryId" class ="form-control">
  36. @foreach(Category item in (ViewBag.CategoryId as SelectList).Items)
  37. {
  38. <option value="@item.Id">@item.Name</option>
  39. if (item.Categories != null && item.Categories.Count > 0)
  40. {
  41. foreach (var subCat in item.Categories)
  42. {
  43. <option value="@subCat.Id">--@subCat.Name</option>
  44. }
  45. }
  46. }
  47. </select>
  48. </div>
  49. </div>
  50. <div class="form-group">
  51. <label asp-for="Product.Description" class="col-md-2 control-label"></label>
  52. <div class="col-md-10">
  53. <input asp-for="Product.Description" class="form-control" />
  54. <span asp-validation-for="Product.Description" class="text-danger" />
  55. </div>
  56. </div>
  57. <div class="form-group">
  58. <label asp-for="Product.Title" class="col-md-2 control-label"></label>
  59. <div class="col-md-10">
  60. <input asp-for="Product.Title" class="form-control" />
  61. <span asp-validation-for="Product.Title" class="text-danger" />
  62. </div>
  63. </div>
  64. <div class="form-group">
  65. <div class="col-md-offset-2 col-md-10">
  66. <input type="submit" value="Create" class="btn btn-default" />
  67. </div>
  68. </div>
  69. </div>
  70. </form>
  71.  
  72. <div>
  73. <a asp-action="Index">Back to List</a>
  74. </div>
  75.  
  76. @section Scripts {
  77. @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
  78. }
  79.  
  80. [Bind("Product,Product.Id,Product.CategoryId,Product.Description,Product.Title")]
  81.  
  82. [Bind("Product.Id","Product.CategoryId","Product.Description","Product.Title")]
Add Comment
Please, Sign In to add comment