C# 使用项来自不同模型的Viewmodel时,在何处添加验证数据注释属性?

C# 使用项来自不同模型的Viewmodel时,在何处添加验证数据注释属性?,c#,asp.net-mvc,validation,C#,Asp.net Mvc,Validation,我在SQL Server中有多个外键的链接表。我正在使用视图模型在EF数据库中首先创建、编辑和删除,以便在数据库更改时使用验证属性。我使用了数据注释,但未验证映射的id列 现在,在这种情况下,在哪里添加一个验证属性,我可以在发布时为每一列获得正确的响应 我不会在从数据库生成的模型中使用验证和注释属性。那么在哪里添加它们,还需要做什么 请查看这些从数据库生成的模型类以及视图模型 public partial class Student { public Student() {

我在SQL Server中有多个外键的链接表。我正在使用视图模型在EF数据库中首先创建、编辑和删除,以便在数据库更改时使用验证属性。我使用了数据注释,但未验证映射的id列

现在,在这种情况下,在哪里添加一个验证属性,我可以在发布时为每一列获得正确的响应

我不会在从数据库生成的模型中使用验证和注释属性。那么在哪里添加它们,还需要做什么

请查看这些从数据库生成的模型类以及视图模型

public partial class Student
{
    public Student()
    {
        this.Resumes = new HashSet<Resume>();
        this.StudentSkills = new HashSet<StudentSkill>();
    }

    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public int CountryId { get; set; }
    public int CityId { get; set; }
    public System.DateTime DateOfBirth { get; set; }
    public System.Guid ResumeId { get; set; }

    public virtual Country Country { get; set; }

    public virtual ICollection<StudentSkill> StudentSkills { get; set; }
}

public partial class Skill
{
    public Skill()
    {
        this.StudentSkills = new HashSet<StudentSkill>();
    }

    public int SkillId { get; set; }
    public string SkillName { get; set; }
    public bool IsSelected { get; set; }

    public virtual ICollection<StudentSkill> StudentSkills { get; set; }
}

public partial class StudentSkill
{
    public int StudentSkillsId { get; set; }
    public int SkillId { get; set; }
    public int StudentId { get; set; }

    public virtual Skill Skill { get; set; }
    public virtual Student Student { get; set; }
}

public partial class City
{
    public int CityId { get; set; }
    public string CityName { get; set; }
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}

public partial class Country
{
    public int CountryId { get; set; }
    public string CountryName { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

public partial class Resume
{
    public System.Guid ResumeId { get; set; }
    public string ResumeName { get; set; }
    public string ResumeExtention { get; set; }
    public int StudentId { get; set; }

    public virtual Student Student { get; set; }
}
公立部分班学生
{
公立学生()
{
this.Resumes=newhashset();
this.StudentSkills=new HashSet();
}
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
public int CountryId{get;set;}
public int CityId{get;set;}
public System.DateTime出生日期{get;set;}
public System.Guid ResumeId{get;set;}
公共虚拟国家{get;set;}
公共虚拟ICollection学生技能{get;set;}
}
公共部分课技能
{
公共技能()
{
this.StudentSkills=new HashSet();
}
公共int SkillId{get;set;}
公共字符串SkillName{get;set;}
公共布尔值被选为{get;set;}
公共虚拟ICollection学生技能{get;set;}
}
公共部分班级学生技能
{
public int StudentSkillsId{get;set;}
公共int SkillId{get;set;}
公共int StudentId{get;set;}
公共虚拟技能技能{get;set;}
公共虚拟学生学生{get;set;}
}
公共部分阶级城市
{
public int CityId{get;set;}
公共字符串CityName{get;set;}
public int CountryId{get;set;}
公共虚拟国家{get;set;}
}
公共部分阶级国家
{
public int CountryId{get;set;}
公共字符串CountryName{get;set;}
公共虚拟ICollection学生{get;set;}
}
公开部分课程简历
{
public System.Guid ResumeId{get;set;}
公共字符串名称{get;set;}
公共字符串恢复扩展{get;set;}
公共int StudentId{get;set;}
公共虚拟学生学生{get;set;}
}
查看模型类:

public class StudentViewModel
{
    [Required(ErrorMessage ="You must enter your name")]
    public string StudentName { get; set; }
    [Required(ErrorMessage = "You must select a country")]
    public int CountryId { get; set; }
    [Required(ErrorMessage = "You must select a country")]
    public IEnumerable<Country> Country { get; set; }
    [Required(ErrorMessage = "You must select a city")]
    public int CityId { get; set; }
    [Required(ErrorMessage = "You must select a city")]
    public IEnumerable<City> City { get; set; }
    [Required(ErrorMessage = "Choose at least one skill")]
    public int SkillId { get; set; }
    [Required(ErrorMessage = "Choose at least one skill")]
    public List<Skill> Skills { get; set; }
    [Required(ErrorMessage = "You must upload your resume")]
    public List<Resume> Resumes { get; set; }
    [Required(ErrorMessage = "You must enter Birth Date")]

    public System.DateTime DateOfBirth { get; set; }
}
公共类学生视图模型
{
[必需(ErrorMessage=“您必须输入您的姓名”)]
公共字符串StudentName{get;set;}
[必需(ErrorMessage=“您必须选择一个国家”)]
public int CountryId{get;set;}
[必需(ErrorMessage=“您必须选择一个国家”)]
公共IEnumerable国家{get;set;}
[必需(ErrorMessage=“您必须选择一个城市”)]
public int CityId{get;set;}
[必需(ErrorMessage=“您必须选择一个城市”)]
公共IEnumerable城市{get;set;}
[必需(ErrorMessage=“选择至少一项技能”)]
公共int SkillId{get;set;}
[必需(ErrorMessage=“选择至少一项技能”)]
公共列表技能{get;set;}
[必需(ErrorMessage=“您必须上传您的简历”)]
公共列表恢复{get;set;}
[必需(ErrorMessage=“您必须输入出生日期”)]
public System.DateTime出生日期{get;set;}
}

这对我来说很合适。在控制器HttpPost方法中,您需要在第一行中执行以下操作:

[HttpPost]
public ActionResult Edit(MyModel mymodel){
if (!ModelState.IsValid){
return View(mymodel);
}
else {    
//do business logic
return RedirectToAction("Index"); 
     }
}

但是这种验证永远不会检查数据库中是否存在有效的id,也不会对业务域进行任何验证,您必须自己实现这些验证。

这里只是一个想法。也许下面的文章可以给出一个想法,它来自微软的论坛,所以给它一个概览:它说你有你将不得不在模式和视图模型之间映射。如果你发现了什么,或者上面的文章帮了你的忙,回信吧。它为城市和国家工作,谢谢你!但我需要为其他项目添加更多逻辑@Jojess你到底想完成什么?你在哪里失败?正如@vasilisdmr所写的,您还需要将ViewModels映射到EF POCO类,我使用AutoMapper。关于您的问题有什么消息吗?@vasilisdmr还没有!我已经在viewmodel类中添加了验证,有些正在工作,但有些仍然没有受到验证的影响,比如文件和出生日期。您是否尝试了我从原始注释中提出的建议?