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.