When creating a post an error occurs enter image description here

Here is the implementation of the post itself

[Authorize] [HttpGet] public ActionResult AddNewPost() { List<int> numlist = new List<int>(); int num = 0; var posts = _blogRepository.GetPosts(); if (posts.Count != 0) { foreach (var post in posts) { var postid = post.Id; Int32.TryParse(postid, out num); numlist.Add(num); } numlist.Sort(); num = numlist.Last(); num++; } else { num = 1; } var newid = num.ToString(); PostViewModel model = new PostViewModel(); model.ID = newid; return View(model); } [Authorize] [HttpPost] [ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult AddNewPost(PostViewModel model) { var post = new Post { Id = model.ID, Body = model.Body, Meta = model.Meta, PostedOn = DateTime.Now, Published = true, ShortDescription = model.ShortDescription, Title = model.Title, UrlSeo = model.UrlSeo }; _blogRepository.AddNewPost(post); return RedirectToAction("EditPost", "Blog", new { slug = model.UrlSeo }); } 

enter image description here

    1 answer 1

    There was no object satisfying the condition in Where . Check the result of FirstOrDefault for null .

     var found = _context.Posts.Where(...).FirstOrDefault(); return (found == null)? null : found.Id; 

    Update

    Once again - the Post object was not found. GetPostIdBySlug returned null as a postid . For this postid no Post was found. The post variable is null .

    • I already did this: return_context.Posts.Where (x => x.UrlSeo == slug) .FirstOrDefault () ?. Id; Nothing happened (Another error appeared - Khmulko Pavel
    • @KhmulkoPavel And what was compiled? - Igor
    • I did as you said and now a new error. threw off the top screen - Khmulko Pavel
    • Nuuu and how to fix? - Khmulko Pavel
    • @KhmulkoPavel How do I know how to fix? This is your program. In the string.IsNullOrEmpty(Model.ID) check string.IsNullOrEmpty(Model.ID) , and output a message that the corresponding post was not found. - Igor