コード例 #1
0
        public async Task <IActionResult> SaveForReplaceMode([Bind("CategoryId,Name")] Category category)
        {
            //https://stackoverflow.com/questions/35202804/submitting-a-razor-form-using-jquery-ajax-in-mvc6-using-the-built-in-functionali
            try
            {
                if (ModelState.IsValid)
                {
                    //add
                    if (category.CategoryId == 0)
                    {
                        _context.Add(category);
                        await _context.SaveChangesAsync();

                        _result = Result.Ok(MessageHelper.Save);
                    }
                    else if (category.CategoryId > 0) //edit
                    {
                        _context.Update(category);
                        await _context.SaveChangesAsync();

                        _result = Result.Ok(MessageHelper.Update);
                    }

                    return(Content(ModalHelper.Content(_result)));
                }

                return(Content(ModalHelper.ContentModelError(ModelState)));
            }
            catch (Exception ex)
            {
                return(Content(ModalHelper.ContentError(ex)));
            }
        }