View 如何在具有aspnet核心的视图中绑定嵌套属性

View 如何在具有aspnet核心的视图中绑定嵌套属性,view,bind,viewmodel,core,View,Bind,Viewmodel,Core,我开始学习AspNetCore,我想知道如何在视图中绑定嵌套属性 我有两个目标 public partial class Club { public Guid Id { get; set; } public string IdUser { get; set; } public string Name { get; set; } public string Position { get; set; } public virtual ApplicationUs

我开始学习AspNetCore,我想知道如何在视图中绑定嵌套属性

我有两个目标

public partial class Club
{
    public Guid Id { get; set; }
    public string IdUser { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    public virtual ApplicationUser IdUserNavigation { get; set; }
}

public class UserViewModel
{
    public string Id { get; set; }
    public string UserName { get; set; }
    [DataType(DataType.Password)]
    public string Password { get; set; }
    [Display(Name = "Confirm Password")]
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }

    public Club Club { get; set; }

    public List<SelectListItem> ApplicationRoles { get; set; }

    [Display(Name = "Role")]
    public string ApplicationRoleId { get; set; }
}
公共部分班级俱乐部
{
公共Guid Id{get;set;}
公共字符串IdUser{get;set;}
公共字符串名称{get;set;}
公共字符串位置{get;set;}
公共虚拟应用程序用户IdUserNavigation{get;set;}
}
公共类UserViewModel
{
公共字符串Id{get;set;}
公共字符串用户名{get;set;}
[数据类型(数据类型.密码)]
公共字符串密码{get;set;}
[显示(Name=“确认密码”)]
[数据类型(数据类型.密码)]
公共字符串ConfirmPassword{get;set;}
公共字符串名称{get;set;}
公共字符串电子邮件{get;set;}
公共俱乐部{get;set;}
公共列表应用程序角色{get;set;}
[显示(Name=“Role”)]
公共字符串ApplicationRoleId{get;set;}
}
现在我想将UserViewModel绑定到视图

    @model TestingViewModels.Users.UserViewModel
    @using Testing.ViewModels.Roles
<form asp-action="AddUser" role="form">
    @await Html.PartialAsync("_ModalHeader", new ModalHeader { Heading = "Add User" })
    <div class="modal-body form-horizontal">
        <div class="row">
            <div class="col-lg-6">
                <div class="form-group">
                    <label asp-for="Name" class="col-lg-3 col-sm-3 control-label"></label>
                    <div class="col-lg-6">
                        <input asp-for="Name" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="Email" class="col-lg-3 col-sm-3 control-label"></label>
                    <div class="col-lg-6">
                        <input asp-for="Email" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="ApplicationRoleId" class="col-lg-3 col-sm-3 control-label"></label>
                    <div class="col-lg-6">
                        <select asp-for="ApplicationRoleId" asp-items="@Model.ApplicationRoles" class="form-control">
                            <option>Please select</option>
                        </select>
                    </div>
                </div>
            </div>
            ....    

            <div class="col-lg-6">
                <div class="form-group">
                    <!--THIS DON´T WORK-->
                    <label asp-for="Club.Name" class="col-lg-3 col-sm-3 control-label"></label>
                    <div class="col-lg-6">
                        <input asp-for="Club.Name" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                <!--THIS DON´T WORK-->
                    <label asp-for="Club.Position" class="col-lg-3 col-sm-3 control-label"></label>
                    <div class="col-lg-6">
                        <input asp-for="Club.Position" class="form-control" />
                    </div>
                </div>
            </div>

        </div>
    </div>
    @await Html.PartialAsync("_ModalFooter", new ModalFooter { })
</form> 
@model TestingViewModels.Users.UserViewModel
@使用Testing.ViewModels.Roles
@等待Html.partialSync(“_ModalHeader”,new ModalHeader{Heading=“Add User”})
请选择
....    
@等待Html.partialsync(“_ModalFooter”,new ModalFooter{})
如何绑定俱乐部名称和俱乐部位置

提前谢谢


Jolynice

一旦对象加载到内存中,您就可以在viewmodel中引用它。 EF的一个特点是加载通常需要显式加载

例如: (在本例中,引用的实体是单个实体而不是集合)

\u dbContext.Entity(club).Reference(a=>a.IdUserNavigation.Load();
为了完整性,对于集合(项目列表),让我们假设您的用户属性是一个用户列表,如

public ICollection IdUserNavigations{get;set;}

\u dbContext.Entity(club.Collection)(a=>a.IdUserNavigations.Load();
请注意,集合用于枚举,引用用于单个实体

执行此操作后,您可以在传递viewmodel时访问视图中的实体

引用模型的方式与引用任何类的方式相同



Thansk Marc,请随时解释。@jolynice。