简体   繁体   English

.NET Core 使用 AppUser 作为另一个模型中的属性

[英].NET Core use AppUser as attribute in another model

So what I'm trying to do is associate an author with a post.所以我要做的是将作者与帖子相关联。 The class AppUser extends from IdentityUser. AppUser 类从 IdentityUser 扩展而来。 I'm trying to set the author of each post as an AppUser, but I'm having some problems.我试图将每篇文章的作者设置为 AppUser,但我遇到了一些问题。 In the database Author doesn't show up instead it's AuthorId, why is that?在数据库中,作者没有出现,而是 AuthorId,这是为什么? However it seems looks lika i can reach the AppUser from the post, I get no error or warnings.但是看起来我可以从帖子中访问 AppUser,我没有收到错误或警告。 But i Still can't view the author name with help of the post, can someone please explain why?但是我仍然无法在帖子的帮助下查看作者姓名,有人可以解释原因吗?

Post Model岗位模型

public class Post
{
    .....
    .....

    public AppUser Author { get; set; }

}

AppUser应用用户

public class AppUser : IdentityUser
{
}

Controller控制器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(Post post)
    {

        if (ModelState.IsValid)
        {

            AppUser appUser = await userManager.GetUserAsync(HttpContext.User);
            post.Author = appUser;

            string slug = userProject.Name.ToLower().Replace(" ", "-");
            post.Slug = slug;

            context.Add(post);
            await context.SaveChangesAsync();

            return RedirectToAction("Index");
        }
        return View(post);
    }

View看法

This is how I'm trying to print the author name in a view, however it just leaves the space empty, I can show all the other attributes of the model in the same way.这就是我试图在视图中打印作者姓名的方式,但是它只是将空间留空,我可以以相同的方式显示模型的所有其他属性。

@Html.DisplayFor(modelItem => item.Author.UserName)

Why won't the author UserName show and why will the attribute show up at AuthorId instead of author?为什么不显示作者用户名,为什么属性会显示在 AuthorId 而不是作者?

Thanks!谢谢!

Edit - added index action编辑 - 添加索引操作

    public IActionResult Index()
    {
        var posts= context.Posts;
        return View(posts);
    }

You should add AuthorId to your Post model.您应该将AuthorId添加到您的Post模型。

public class Post
{
    public string AuthorId { get; set; }

    [ForeignKey("AuthorId")]
    public AppUser Author { get; set; }
}

and just set AuthorId to appUser.Id并将AuthorId设置为appUser.Id

post.AuthorId = appUser.Id;

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

相关问题 如何在 Asp.Net Core 2.2 中使用具有自定义 model 绑定的 [FromHeader] 属性 - How to use [FromHeader] attribute with custom model binding in Asp.Net Core 2.2 ASP.NET CORE / 添加时遇到的问题。AddSignInManager <signinmanager<appuser> &gt;(); 在启动 class </signinmanager<appuser> - ASP.NET CORE / Issue encountered when adding .AddSignInManager<SignInManager<AppUser>>(); in the Startup class ASP Net MVC-具有模型属性引用另一个模型属性 - asp net mvc - have a model attribute reference another model attribute 提取模型/对象以在.Net Core中的局部视图中使用 - Fetching a model/object to use in a partialview in .Net Core 枚举的JsonConverter属性打破了asp.net核心模型绑定 - JsonConverter attribute on enum breaks asp.net core model binding ASP Net Core模型验证Range属性被忽略 - ASP Net Core model validation Range attribute is ignored 在ASP.NET CORE中将属性路由值转换为Model / FromBody参数 - Attribute Routing Values into Model/FromBody Parameter in ASP.NET CORE 将模型值设置为小写的 ASP.NET Core 2.1 属性 - ASP.NET Core 2.1 Attribute to set model value to lowercase .NET 核心 API - 为什么嵌套 model 中的文件属性接收 Z37A6259CC0C1DAE299A7866489DFF0 - .NET Core API - Why is file attribute in nested model receives null? 基于具有属性的另一个属性的 ASP.NET Core 检查验证 - ASP.NET Core Check validation based on another property with attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM