title: Edit action with Dependency Injection ASP.Net Core MVC
body: <p>I'm trying to make Edit action using DI.</p>
  <p>I am using <strong>Manage</strong> View to Edit or Delete objects.</p>
  <p><strong>IPost</strong> interface:</p>
  <pre class="lang-cs prettyprint-override"><code>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&lt;Post&gt; GetAll();
          IEnumerable&lt;Post&gt; GetFilteredPosts(int id, string searchQuery);
          IEnumerable&lt;Post&gt; GetFilteredPosts(string searchQuery);
          IEnumerable&lt;Post&gt; GetPostsByCategory(int id);
          IEnumerable&lt;Post&gt; GetLatestPosts(int n);
  
  
          Task Add(Post post);
          Task Delete(int id);
          void Update(Post post);
  
          Task AddReply(PostReply reply);
      }
  }
  </code></pre>
  <p><strong>PostService</strong> Update Method:</p>
  <pre class="lang-cs prettyprint-override"><code>public void Update(Post post)
          {
              _context.Posts.Update(post);
              _context.SaveChanges();
          }
  </code></pre>
  <p>PostController Edit GET and POST Action:</p>
  <pre class="lang-cs prettyprint-override"><code>public IActionResult Edit(int postId)
          {
              Post postt = _postService.GetById(postId);
              return View(postt);
          }
  
  [HttpPost]
          public IActionResult Edit(Post post)
          {
              _postService.Update(post);
              return RedirectToAction(&quot;Manage&quot;);
          }
  </code></pre>
  <p><strong>Post</strong> Model that <strong>Update</strong> and <strong>Edit</strong> methods refers to:</p>
  <pre class="lang-cs prettyprint-override"><code>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&lt;PostReply&gt; Replies { get; set; }
      }
  }
  </code></pre>
  <p>This is my <strong>Edit</strong> View:</p>
  <pre class="lang-html prettyprint-override"><code>@model collector_forum.Data.Models.Post
  
  @{
      ViewData[&quot;Title&quot;] = &quot;Edit Post&quot;;
  }
  &lt;div class=&quot;container body-content&quot;&gt;
      &lt;div class=&quot;row sectionHeader&quot;&gt;
          &lt;div class=&quot;sectionHeading&quot;&gt;New Post&lt;/div&gt;
          &lt;div class=&quot;sectionDescription&quot;&gt;
              &lt;p&gt;
                  Please read the Forum Guidelines before creating a new post.
  
                  @if (!Context.User.Identity.IsAuthenticated)
                  {
                      &lt;span&gt;You must be a &lt;a asp-controller=&quot;Account&quot; asp-action=&quot;Register&quot;&gt;registered member&lt;/a&gt; to create a new post.&lt;/span&gt;}
              &lt;/p&gt;
          &lt;/div&gt;
          @if (Context.User.Identity.IsAuthenticated)
          {
              &lt;div class=&quot;createPost&quot;&gt;
                  &lt;div class=&quot;row createPost&quot;&gt;
                      &lt;div class=&quot;createPostSection&quot;&gt;
                          &lt;div class=&quot;authorBlock&quot;&gt;
                              You're submitting this post as &lt;strong&gt;@Model.User.UserName&lt;/strong&gt;
                          &lt;/div&gt;
                          &lt;form asp-action=&quot;Edit&quot; method=&quot;post&quot; id=&quot;addPostForm&quot;&gt;
                              &lt;div class=&quot;form-group&quot;&gt;
                                  &lt;label asp-for=&quot;Title&quot;&gt;&lt;/label&gt;
                                  &lt;input asp-for=&quot;Title&quot; class=&quot;form-control&quot; /&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;form-group&quot;&gt;
                                  &lt;label asp-for=&quot;Content&quot;&gt;&lt;/label&gt;
                                  &lt;input asp-for=&quot;Content&quot; class=&quot;form-control&quot; /&gt;
                              &lt;/div&gt;
                              &lt;button type=&quot;submit&quot; id=&quot;submitPostBtn&quot; class=&quot;btn btn-submitPost&quot;&gt;
                                  Edit Post
                              &lt;/button&gt;
                              &lt;input asp-for=&quot;Category.Id&quot; type=&quot;hidden&quot; /&gt;
                          &lt;/form&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          }
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p>I follow this <a href="https://www.youtube.com/watch?v=Ig9AkVY84bM&amp;ab_channel=ASP.NETMVC" rel="nofollow noreferrer">tutorial</a> and can't replace postId - it's creating new ID, and can't pass non-edited values like userId/UserName.</p>
  <p>When I confirm edited post I get NullReferenceException from Database - <a href="https://i.stack.imgur.com/7MBOv.png" rel="nofollow noreferrer">errorpage</a></p>
  <p>And this is my table view after &quot;changes&quot; - <a href="https://i.stack.imgur.com/HnMm4.png" rel="nofollow noreferrer">DBtable</a></p>
  <p>How to just edit Title and Content of this post without touching other fields?</p>
  <p>If You need more code, let me know.</p>