【问题标题】:Where to add validation data annotation attribute while using Viewmodel whose items are coming from different model?使用来自不同模型的 Viewmodel 时在哪里添加验证数据注释属性?
【发布时间】: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


【解决方案1】:

这对我来说看起来不错。在您的控制器 HttpPost 方法中,您需要在第一行执行以下操作:

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

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

【讨论】:

  • 它适用于城市和国家,谢谢!但我需要为其他项目添加更多逻辑。 @jojess
  • 你到底想完成什么,你在哪里失败了?正如@vasilisdmr 所写,您还需要将您的 ViewModel 映射到 EF POCO 类,为此我使用 AutoMapper。
  • 有任何关于您的问题的消息吗?
  • @vasilisdmr 还没有!我在 viewmodel 类中添加了验证,有些正在工作,但有些仍然没有受到影响,比如 files 和 dateOfBirth
  • 您是否尝试过我在原始评论中的建议?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多