예제 #1
0
 public IActionResult Create([Bind("Id,Title,Perex,Content,AuthorId,CreatedAt")] Post post)
 {
     if (ModelState.IsValid)
     {
         _context.Add(post);
         _context.SaveChanges();
         return(RedirectToAction(nameof(Index)));
     }
     return(View(post));
 }
예제 #2
0
        public async Task <IActionResult> Post([Bind("ID, Title, Author, Content")] Post post)
        {
            if (ModelState.IsValid)
            {
                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(GetAll)));
            }

            return(View(post));
        }
예제 #3
0
        public async Task <ActionResult> Create(Post post)
        {
            // Manual Requests body to object binding caused by TS & C# dates incompatibility.
            try
            {
                string postObject;
                using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
                {
                    postObject = await reader.ReadToEndAsync();
                }
                post = JsonConvert.DeserializeObject <Post>(postObject);

                // according to TypeScript Date and C# DateTime incompatibility, have to set the creationDate on the server-side

                post.PublicationDate = DateTime.Now;
                _ctx.Add(post);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(Ok(post));
        }