Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET核心MVC中访问SQL数据库中的用户ICollection_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 在ASP.NET核心MVC中访问SQL数据库中的用户ICollection

C# 在ASP.NET核心MVC中访问SQL数据库中的用户ICollection,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,在我的Models文件夹中,我有一个I集合,类型为项 public class ApplicationUser : IdentityUser { [PersonalData] [Column] public string FirstName { get; set; } [PersonalData] [Column] public string LastName { get; set; } [PersonalData] [Colu

在我的
Models
文件夹中,我有一个
I集合
,类型为

public class ApplicationUser : IdentityUser
{
    [PersonalData]
    [Column]
    public string FirstName { get; set; }

    [PersonalData]
    [Column]
    public string LastName { get; set; }

    [PersonalData]
    [Column]
    public ICollection<Item> Items { get; set; }
}
当我更新数据库时,我得到了以下信息:

我为
用户
和用户的
ICollection
创建了一个表

现在我的问题是如何在razor页面中访问或操作这段数据。到目前为止,我的剃须刀页面和它的后端都有这个

<form method="post">

<div>Item Name</div>
<input asp-for="Input.ItemName" />

<div>Item Description</div>
<input asp-for="Input.ItemDescription" />

<button type="submit">Save</button>

项目名称
项目说明
拯救
使用系统;
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用ArrayCheckCore.Areas.Identity.Data;
使用Microsoft.AspNetCore.Identity;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Microsoft.Extensions.Logging;
命名空间ArrayCheckNetCore.Areas.Identity.Pages.Account.Manage
{
公共类PersonalDataModel:PageModel
{
私有只读用户管理器_UserManager;
专用只读签名管理器\u签名管理器;
专用只读ILogger\u记录器;
公共个性化数据模型(
用户管理器用户管理器,
SignInManager SignInManager,
ILogger(记录器)
{
_userManager=userManager;
_signInManager=signInManager;
_记录器=记录器;
}
[BindProperty]
公共输入模型输入{get;set;}
公共类输入模型
{
公共字符串ItemName{get;set;}
公共字符串ItemDescription{get;set;}
}
专用异步任务LoadAsync(项目用户)
{
var itemsname=user.ItemName;
var itemsdescription=user.itemsdescription;
输入=新输入模型
{
ItemName=itemsname,
ItemDescription=itemsdescription
};
}
公共异步任务OnGetAsync()
{
var user=await\u userManager.GetUserAsync(用户);
if(user==null)
{
返回NotFound($“无法加载ID为“{{u userManager.GetUserId(user)}”的用户”);
}
//等待加载异步(用户);
返回页();
}
公共异步任务OnPostAsync()
{
var user=await\u userManager.GetUserAsync(用户);
if(user==null)
{
返回NotFound($“无法加载ID为“{{u userManager.GetUserId(user)}”的用户”);
}
如果(!ModelState.IsValid)
{
//等待加载异步(用户);
返回页();
}
user.ItemName=Input.ItemName;
user.itemsdescription=Input.itemsdescription;
wait_userManager.UpdateAsync(用户);
//等待(u signInManager.RefreshSignInAsync)(用户);;
返回重定向Topage();
}
}
}

如果有人知道我如何访问这些属性,谢谢。

我假设您正在将Entity Framework Core用于此应用程序,因此,您必须通过访问DbContext中的ApplicationUser表或使用UserManager类来加载项集合。无论哪种方式,都可以使用Include()扩展方法和IQueryable用户来加载相关数据。更容易显示示例


公共类索引模型:PageModel
{
/// 
///注入DbContext
/// 
只读ApplicationDbContext上下文;
/// 
///注入用户管理器
/// 
只读用户管理器


编辑以更新IndexModel并包含razor页面的html

你所说的
用户ICollection
用户ICollection
是什么意思?为什么它与razor页面有关?你能在控制器/页面代码中做类似的事情吗?我完全不知道你的问题是什么。通过使用你的模型,因为项目继承自应用程序ApplicationUser表,迁移后,它将生成一个包含所有属性的AspNetUser,可能是因为您有其他配置。从项模型中删除继承后,它将生成项表。之后,您的意思是在管理项时要访问ApplicationUser的FirstName和LastName属性吗?如果在这种情况下,您可以基于访问项目。如何在前端(Razor页面)上访问这些属性?如何向该页面添加数据?类似于表单中的数据?您需要一个表单页面来处理此模型的更新。通常情况下,表格页面会有按钮或链接,将您带到页面,您可以在该页面上修改单个模型,然后将其提交到数据库。这将有助于您在文档显示时完成该部分如何添加和提交到表单,它并没有真正讨论如何提交到ICollection类型表单。我已经尝试了我能想到的一切,但什么也没有。我似乎也无法访问Item表中的ItemName和ItemDescription属性。如果有人知道如何访问这些属性,谢谢。
<form method="post">

<div>Item Name</div>
<input asp-for="Input.ItemName" />

<div>Item Description</div>
<input asp-for="Input.ItemDescription" />

<button type="submit">Save</button>
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ArrayCheckNetCore.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace ArrayCheckNetCore.Areas.Identity.Pages.Account.Manage
{
    public class PersonalDataModel : PageModel
    {
        private readonly UserManager<Item> _userManager;
        private readonly SignInManager<ApplicationUser> _signInManager;
        private readonly ILogger<PersonalDataModel> _logger;

        public PersonalDataModel(
            UserManager<Item> userManager,
            SignInManager<ApplicationUser> signInManager, 
            ILogger<PersonalDataModel> logger)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _logger = logger;
        }

        [BindProperty]
        public InputModel Input { get; set; }

        public class InputModel
        {
            public string ItemName { get; set; }
            public string ItemDescription { get; set; }
        }

        private async Task LoadAsync(Item user)
        {
            var itemsname = user.ItemName;
            var itemsdescription = user.ItemDescription; 

            Input = new InputModel
            {
                ItemName = itemsname, 
                ItemDescription = itemsdescription 
            };
        }

        public async Task<IActionResult> OnGetAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            //await LoadAsync(user);
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            if (!ModelState.IsValid)
            {
                //await LoadAsync(user);
                return Page();
            }

            user.ItemName = Input.ItemName;
            user.ItemDescription = Input.ItemDescription; 

            await _userManager.UpdateAsync(user);
            //await _signInManager.RefreshSignInAsync(user); 
            return RedirectToPage();
        }
    }
}
@page "/"
@model IndexModel

@{
    ViewData["Title"] = "Home page";
}

<table class="table table-hover table-bordered">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Phone Number</th>
            <th>Items</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var user in Model.UsersWithItemsOption1)
        {
        <tr>
            <td>@user.FirstName</td>
            <td>@user.LastName</td>
            <td>@user.PhoneNumber</td>
            <td>
                @if (user.Items != null)
                {
                    ///Prints out all the Item Names in a single string seperated by a comma
                    string.Join(", ", user.Items.Select(a => a.ItemName).ToList());
                }
            </td>
        </tr>
        }
    </tbody>
</table>