示例#1
0
        public async Task <IActionResult> Create([Bind("Name,IdCategory,Price")] Product product)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    await productComponent.Create(product);

                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                //Log the error
                ModelState.AddModelError("", ex.Message);
            }

            var categories = await categoryComponent.GetAllCategories();

            if (categories == null)
            {
                RedirectToAction("Index");
            }

            ViewBag.CategoryList = new SelectList(categories, "Id", "Name");

            return(View(product));
        }
示例#2
0
        public async Task <JsonResult> CreateProduct([FromBody] Product newProduct)
        {
            var newProductId = await productComponent.Create(newProduct);

            return(Json(newProductId));
        }