Example #1
0
        /**************************************************************************************
        *   Function Name    : PostEditEdit
        *   Description      : Receive the modified parameters from the page, and update the individual attributes of the article according to the id.
        *   Caution          : Parameter    :   int? id             -> the id of article
        *                                       Article article     -> the attributes of the article
        *                      Return       :   The json result of edit
        **************************************************************************************/
        public async Task <JsonResult> PostEdit(int id, [Bind("Id,Title,Author,Date,Content,AttachFile")] Article article)
        {
            if (id != article.Id)
            {
                return(Json(new { result = 0, msg = "article id error" }));
            }

            if (ModelState.IsValid)
            {
                try {
                    _context.Update(article);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException) {
                    if (!ArticleExists(article.Id))
                    {
                        return(Json(new { result = 0, msg = "article not find" }));
                    }
                    else
                    {
                        return(Json(new { result = 0, msg = "unexcepted error" }));
                    }
                }
                return(Json(new { result = 1, msg = "" }));
            }

            var errorValue = ModelState.Where(a => a.Value.Errors.Count > 0).First();

            return(Json(new { result = 0, msg = "Input: " + errorValue.Key + " is not valid" }));
        }