public async Task <ApplicationResult <SubCategoryDto> > Create(CreateSubCategoryViewModel model)
        {
            try
            {
                var user = await _userManager.FindByIdAsync(model.CreatedById);

                SubCategory mapSubCategory = _mapper.Map <SubCategory>(model);
                mapSubCategory.CreatedById = model.CreatedById;
                mapSubCategory.CreatedBy   = user.UserName;
                mapSubCategory.CategoryId  = model.CategoryId;
                _context.SubCategories.Add(mapSubCategory);
                await _context.SaveChangesAsync();

                ApplicationResult <SubCategoryDto> result = new ApplicationResult <SubCategoryDto>
                {
                    Result    = _mapper.Map <SubCategoryDto>(mapSubCategory),
                    Succeeded = true
                };

                return(result);
            }
            catch (Exception e)
            {
                ApplicationResult <SubCategoryDto> result = new ApplicationResult <SubCategoryDto>();
                result.Succeeded    = false;
                result.ErrorMessage = e.Message;
                return(result);
            }
        }
        public ActionResult Create(int categoryId)
        {
            var model = new CreateSubCategoryViewModel {
                CategoryId = categoryId
            };

            return(View(model));
        }
        public ActionResult Create()
        {
            var categories = this.categoryService.GetAllCategories();
            var model      = new CreateSubCategoryViewModel
            {
                Categories = categories
            };

            return(this.View(model));
        }
        public async Task <ActionResult> Create(CreateSubCategoryViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var subCategory = new SubCategory {
                    CategoryId = viewModel.CategoryId, Name = viewModel.Name, Description = viewModel.Description, CustomerId = await _customerIdService.GetCustomerId()
                };
                await _subCategoryRepository.Add(subCategory);

                return(RedirectToAction("Details", "Category", new { Id = viewModel.CategoryId }));
            }

            return(View(viewModel));
        }
Exemplo n.º 5
0
        public ActionResult CreateSubCategory([Bind(Include = "CategoryId")] int?categoryId)
        {
            if (categoryId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var viewModel = new CreateSubCategoryViewModel();

            // get the user role
            viewModel.CreatedBy = User.Identity.GetUserId();
            // need to initialise the date. it will get reset when the category is saved to the database with upto date datetime value
            viewModel.CreateDate = DateTime.UtcNow;
            viewModel.ParentId   = categoryId;
            viewModel.Parent     = _db.Categories.Find(categoryId);

            return(View(viewModel));
        }
Exemplo n.º 6
0
        public ActionResult CreateSubCategory([Bind(Include = "Name,Description,ParentId,CreatedBy,CreateDate")] CreateSubCategoryViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var parent   = _db.Categories.Find(viewModel.ParentId);
                var category = new Category();
                category.Name        = viewModel.Name;
                category.Description = viewModel.Description;
                category.CreateDate  = DateTime.UtcNow;
                category.CreatedBy   = User.Identity.GetUserId();
                category.ParentId    = viewModel.ParentId;

                _db.Categories.Add(category);
                _db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            // use this to inspect model state errors
            var errors = ModelState.Values.SelectMany(v => v.Errors);

            return(View(viewModel));
        }
        public async Task <IActionResult> Create(CreateSubCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.CreatedById = User.FindFirst(ClaimTypes.NameIdentifier).Value;
                var result = await _subCategoryService.Create(model);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError(string.Empty, result.ErrorMessage);
            }
            var categoryList = await _categoryService.GetAll();

            ViewBag.CategoryDDL = categoryList.Result.Select(x => new SelectListItem
            {
                Selected = false,
                Text     = x.CategoryName,
                Value    = x.Id.ToString()
            }).ToList();
            return(View(model));
        }