Beispiel #1
0
        /**************************************************************************************
        *   Function Name    : PostCreate
        *   Description      : Receive the attributes of the article , and insert the article into the dataset.
        *   Caution          : Parameter    :   Article article     -> the attributes of the article
        *                      Return       :   The JSON result
        **************************************************************************************/

        public async Task <JsonResult> PostCreate([Bind("Title,Author,Date,Content,AttachFile")] Article article)
        {
            if (ModelState.IsValid)
            {
                try {
                    int id       = 0;
                    int allcount = _context.Article.Count();
                    if (allcount != 0)
                    {
                        id = _context.Article.Max(a => a.Id);
                    }
                    article.Id = id + 1;

                    _context.Add(article);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException) {
                    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" }));
        }