-2

I'm trying to make Edit action using DI.

I am using Manage View to Edit or Delete objects.

IPost interface:

using collector_forum.Data.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace collector_forum.Data
{
    public interface IPost
    {
        Post GetById(int id);
        IEnumerable<Post> GetAll();
        IEnumerable<Post> GetFilteredPosts(int id, string searchQuery);
        IEnumerable<Post> GetFilteredPosts(string searchQuery);
        IEnumerable<Post> GetPostsByCategory(int id);
        IEnumerable<Post> GetLatestPosts(int n);


        Task Add(Post post);
        Task Delete(int id);
        void Update(Post post);

        Task AddReply(PostReply reply);
    }
}

PostService Update Method:

public void Update(Post post)
        {
            _context.Posts.Update(post);
            _context.SaveChanges();
        }

PostController Edit GET and POST Action:

public IActionResult Edit(int postId)
        {
            Post postt = _postService.GetById(postId);
            return View(postt);
        }

[HttpPost]
        public IActionResult Edit(Post post)
        {
            _postService.Update(post);
            return RedirectToAction("Manage");
        }

Post Model that Update and Edit methods refers to:

using System;
using System.Collections.Generic;

namespace collector_forum.Data.Models
{
    public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public DateTime Created { get; set; }

        public virtual ApplicationUser User { get; set; }

        public virtual Category Category { get; set; }

        public virtual IEnumerable<PostReply> Replies { get; set; }
    }
}

This is my Edit View:

@model collector_forum.Data.Models.Post

@{
    ViewData["Title"] = "Edit Post";
}
<div class="container body-content">
    <div class="row sectionHeader">
        <div class="sectionHeading">New Post</div>
        <div class="sectionDescription">
            <p>
                Please read the Forum Guidelines before creating a new post.

                @if (!Context.User.Identity.IsAuthenticated)
                {
                    <span>You must be a <a asp-controller="Account" asp-action="Register">registered member</a> to create a new post.</span>}
            </p>
        </div>
        @if (Context.User.Identity.IsAuthenticated)
        {
            <div class="createPost">
                <div class="row createPost">
                    <div class="createPostSection">
                        <div class="authorBlock">
                            You're submitting this post as <strong>@Model.User.UserName</strong>
                        </div>
                        <form asp-action="Edit" method="post" id="addPostForm">
                            <div class="form-group">
                                <label asp-for="Title"></label>
                                <input asp-for="Title" class="form-control" />
                            </div>
                            <div class="form-group">
                                <label asp-for="Content"></label>
                                <input asp-for="Content" class="form-control" />
                            </div>
                            <button type="submit" id="submitPostBtn" class="btn btn-submitPost">
                                Edit Post
                            </button>
                            <input asp-for="Category.Id" type="hidden" />
                        </form>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

I follow this tutorial and can't replace postId - it's creating new ID, and can't pass non-edited values like userId/UserName.

When I confirm edited post I get NullReferenceException from Database - errorpage

And this is my table view after "changes" - DBtable

How to just edit Title and Content of this post without touching other fields?

If You need more code, let me know.

Piotr
  • 5
  • 4
  • We don't know what's in your `post` variable (inside `Edit(Post post)` action and we don't know how your `_postService.Update(post)` method looks like, so it's impossible to tell what's the problem. – Christoph Lütjen Apr 04 '21 at 16:16
  • I just edit question and add code that you want. I've included the code for the `Update` method – Piotr Apr 04 '21 at 16:30
  • @Piotr Please check https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it on how to fix NullReferenceExceptions. – Progman Apr 04 '21 at 16:52
  • @Piotr Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the full source code you have as a [mcve], which can be compiled and tested by others. The code you have provided is incomplete, as the exception comes from the `HomeController` and its view page. – Progman Apr 04 '21 at 16:59
  • @Progman Why you redirect me to another question that it is not very much related to my problem? My question is about passing or not passing specific field values. Which are declared in Model. Simple question man. I'm newbie in this technology so I 'm here to ask others – Piotr Apr 04 '21 at 17:00
  • @Piotr Please check https://stackoverflow.com/questions/13402227/posting-to-edit-controller-action-not-passing-id-of-model on how to pass variables/values from your model which are not directly editable in your HTML form (like an id). – Progman Apr 04 '21 at 17:12

1 Answers1

1

You don't pass userId to Edit action when you post your form,so when you update Post post,UserId will be null.You can add hidden input with into your form of Edit View:

<input asp-for="User.Id" type="hidden" />

So that you will have user id in Post post of your Edit action.And then you can save it to db.

Yiyi You
  • 6,871
  • 1
  • 2
  • 9