简体   繁体   English

使用依赖注入 ASP.Net Core MVC 编辑操作

[英]Edit action with Dependency Injection ASP.Net Core MVC

I'm trying to make Edit action using DI.我正在尝试使用 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: PostService更新方法:

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

PostController Edit GET and POST Action: PostController 编辑 GET 和 POST 操作:

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: Post Model更新编辑方法指的是:

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.我遵循本教程并且无法替换 postId - 它正在创建新 ID,并且无法传递未编辑的值,例如 userId/UserName。

When I confirm edited post I get NullReferenceException from Database - errorpage当我确认已编辑的帖子时,我从数据库中得到 NullReferenceException - errorpage

And this is my table view after "changes" - DBtable这是我“更改”后的表格视图 - DBtable

How to just edit Title and Content of this post without touching other fields?如何只编辑这篇文章的标题和内容而不触及其他字段?

If You need more code, let me know.如果您需要更多代码,请告诉我。

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:发布表单时,您不会将 userId 传递给Edit操作,因此当您更新Post post时,UserId 将为 null。您可以将隐藏输入添加到您的编辑视图表单中:

<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.这样您就可以在编辑操作的Post post中拥有用户 ID。然后您可以将其保存到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM