Asp.net mvc 在ASP.NET MVC中对多个模型使用ViewModel

Asp.net mvc 在ASP.NET MVC中对多个模型使用ViewModel,asp.net-mvc,entity-framework,entity-framework-6,automapper,asp.net-mvc-viewmodel,Asp.net Mvc,Entity Framework,Entity Framework 6,Automapper,Asp.net Mvc Viewmodel,在ASP.NET MVC中创建具有多个模型(实体)的ViewModel时,我使用以下方法: 学生实体: public class Student { [Key] public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } //Foreign key for Country public int Country

在ASP.NET MVC中创建具有多个模型(实体)的ViewModel时,我使用以下方法:

学生实体:

public class Student
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; } 

    //Foreign key for Country
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}
public class Country
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}
public class StudentViewModel
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; } 

    //Foreign key for Country
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}
public class CountryViewModel
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    //??? Should I use Student entity or StudentViewModel at here >>>
    public virtual ICollection<Student> Students { get; set; }
}
国家/地区实体:

public class Student
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; } 

    //Foreign key for Country
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}
public class Country
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}
public class StudentViewModel
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; } 

    //Foreign key for Country
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}
public class CountryViewModel
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    //??? Should I use Student entity or StudentViewModel at here >>>
    public virtual ICollection<Student> Students { get; set; }
}
国家/地区视图模型:

public class Student
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; } 

    //Foreign key for Country
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}
public class Country
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}
public class StudentViewModel
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; } 

    //Foreign key for Country
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}
public class CountryViewModel
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    //??? Should I use Student entity or StudentViewModel at here >>>
    public virtual ICollection<Student> Students { get; set; }
}
公共类CountryViewModel
{
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
//???我应该在此处使用学生实体还是学生视图模型>>>
公共虚拟ICollection学生{get;set;}
}
在此阶段,我不确定是否应在ViewModel中添加相关模型的实体或ViewModels。我使用Automapper和map entity和viewmodels,但我不想映射如下所示的每个属性,而是希望合并ViewModel类中的模型

CreateMap<Student, StudentViewModel>()
   .ForMember(dest => ...)
   .ForMember(dest => ...)
   .ForMember(dest => ...)
CreateMap()
.FormMember(目标=>…)
.FormMember(目标=>…)
.FormMember(目标=>…)

所以,你能告诉我如何正确地合并viewmodel中的多个实体吗

那很容易,希望这对你有帮助。FormMember(vm=>vm.Name,map=>map.MapFrom(model=>model.Name))在CountryViewModel中使用StudentViewModel,在StudentViewModel中使用CountryViewModel。设置2个映射(国家->国家视图模型和学生->学生视图模型)。Automapper足够智能,无需
FormMember
@AsifRaza即可映射子对象。您的意思是,不需要在ViewModels中使用导航属性,并且可以通过Automapper推断必要的实体属性和模型吗?你能帮我吗。再解释一下?一般来说,组合视图模型。如果是我,我就不会有CountryViewModel——我只会将它展平到StudentViewModel中(也就是说,我会在StudentViewModel中有一个CountryName属性。你不需要添加一堆FormMember子句。这就是Automapper的目的。是的。将视图模型视为组成视图所需的内容。它显然将由各种实体以及构建视图所需的一些其他非实体属性组成。因此在y中我们的GET方法将加载一个viewmodel,在文章中,您将反转更新实体的过程。将验证放在VM中。关于这个主题的文章很多。