【问题标题】:Edit action with Dependency Injection ASP.Net Core MVC使用依赖注入 ASP.Net Core MVC 编辑操作
【发布时间】:2021-04-05 11:21:11
【问题描述】:

我正在尝试使用 DI 进行编辑操作。

我正在使用管理视图来编辑或删除对象。

IPost界面:

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更新方法:

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

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 UpdateEdit 方法所指的模型:

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; }
    }
}

这是我的编辑视图:

@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>

我关注此tutorial 并且无法替换 postId - 它正在创建新 ID,并且无法传递未经编辑的值,例如 userId/UserName。

当我确认已编辑的帖子时,我从数据库中得到 NullReferenceException - errorpage

这是我“更改”后的表格视图 - DBtable

如何只编辑这篇文章的标题和内容而不触及其他字段?

如果您需要更多代码,请告诉我。

【问题讨论】:

  • 我们不知道你的post 变量中有什么(在Edit(Post post) 操作中,我们不知道你的_postService.Update(post) 方法是什么样子的,因此无法判断出什么问题。
  • 我只是编辑问题并添加您想要的代码。我已经包含了Update 方法的代码
  • @Piotr 请查看stackoverflow.com/questions/4660142/…,了解如何修复 NullReferenceExceptions。
  • @Piotr 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 了解如何提高问题的质量。然后edit你的问题包括你拥有的完整源代码作为minimal reproducible example,它可以被其他人编译和测试。您提供的代码不完整,因为异常来自HomeController 及其查看页面。
  • @Progman 为什么将我重定向到另一个与我的问题没有太大关系的问题?我的问题是关于传递或不传递特定字段值。在模型中声明。简单的问题人。我是这项技术的新手,所以我在这里问别人

标签: c# asp.net-core asp.net-core-mvc parameter-passing nullreferenceexception


【解决方案1】:

您在发布表单时不会将 userId 传递给 Edit 操作,因此当您更新 Post post 时,UserId 将为空。您可以将隐藏输入添加到您的编辑视图表单中:

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

这样您就可以在编辑操作的Post post 中拥有用户ID。然后您可以将其保存到数据库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多