【发布时间】:2020-03-08 19:45:49
【问题描述】:
我在 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; }
}
查看模型类:
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; }
}
【问题讨论】:
-
这里只是一个想法。也许下面的文章可以让你知道它来自微软的论坛,所以你看一眼:forums.asp.net/t/…它说你有你必须在Mode和ViewModel之间进行映射。如果您发现了什么,或者上面的文章对您有所帮助,请回信。
标签: c# asp.net-mvc validation