使用复杂模型(虚拟成员)进行 MVC 模型验证

MVC model validation with complex model (virtual member)

提问人:user3166316 提问时间:2/3/2023 最后编辑:Renauser3166316 更新时间:2/3/2023 访问量:38

问:

我有一个模型:

public class Student : AbstractDomainObject 
{
    public Student() 
    {
        Guardians = new HashSet<Guardian>();
    }

    public int Id { get; set; }

    [Required(ErrorMessage = "First Name is required.")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is required.")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Birthdate is required.")]
    public DateTime? DOB { get; set; }

     public virtual ICollection<Guardian> Guardians { get; set; }
}

监护人模型为:

public partial class Guardian : AbstractDomainObject 
{
    private string _homePhone;
    private string _cellPhone;
    public int Id { get; set; }
    
    public int StudentId { get; set; }

    [Required(ErrorMessage = "This field is required.")]
    public string Relationship { get; set; }

    [Required(ErrorMessage = "Guardian First Name is required.")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "This field is required.")]
    public string LastName { get; set; }

    public virtual Student Student { get; set; }
}

在我的 HTML 中,我有

<div class="col-2"><label class="required">First Name:</label></div>
<div class="col-4">
    <input type="text" class="textBox" asp-for="FirstName" id="FirstName" 
     tabindex="4">
     <span class="required">*</span>
     <span asp-validation-for="FirstName" class="text-danger"></span>}

再晚一点

<div class="row with-margin">
<div class="col-4">
    <label for="Guardians.FirstOrDefault<Guardian>().FirstName" class="required">Parent First Name:</label>
    </div>
        <div class="col-4">
            <input type="text" class="textBox" asp-for="Guardians.FirstOrDefault<Guardian>().FirstName" id="ParentFirstName" tabindex="16">
            <span class="required">*</span>
            <span asp-validation-for="Guardians.FirstOrDefault<Guardian>().FirstName" class="text-danger"></span>

                                

当“学生名字”为空且填写了“监护人名字”时,将同时显示“学生名字”(如预期)和“家长名字”(即使其值有效)的验证消息。 检查 HTML 时,两个字段的名称都为 FirstName。改变 Guardian 模型“FirstName”将是残酷的

帮助

. ASP.NET-CORE 验证

评论


答:

0赞 Rena 2/3/2023 #1

当“学生名字”为空且填写了“监护人名字”时,将同时显示“学生名字”(如预期)和“家长名字”(即使其值有效)的验证消息。检查 HTML 时,两个字段的名称都为 FirstName。改变 Guardian 模型“FirstName”将是残酷的

是的,正如您所说,和 将为 attribute 生成相同的值。此外,输入也会生成不正确的名称属性,并且不适用于进一步的表单发布。asp-validation-for="Guardians.FirstOrDefault<Guardian>().FirstName"asp-validation-for="FirstName"data-valmsg-forGuardian

您可以将模型属性更改为 type of 而不是 ,然后您可以在视图中使用这将生成正确的 和 。ListICollectionGuardians[0].FirstNamenamedata-valmsg-for

型:

public class Student : AbstractDomainObject
{
    public Student()
    {
        Guardians = new List<Guardian>(); //change here...
    }

    public int Id { get; set; }
    //.....
    public virtual List<Guardian> Guardians { get; set; }  //change here...
}

视图:

@model Student
<form method="post">
    <div class="col-2"><label class="required">First Name:</label></div>
    <div class="col-4">
        <input type="text" class="textBox" asp-for="FirstName" id="FirstName"
               tabindex="4">
        <span class="required">*</span>
        <span asp-validation-for="FirstName" class="text-danger"></span>
    </div>
    <div class="row with-margin">
        <div class="col-4">
            <label for="Guardians[0].FirstName" class="required">Parent First Name:</label>
        </div>
        <div class="col-4">
            <input type="text" class="textBox" asp-for="Guardians[0].FirstName" id="ParentFirstName" tabindex="16">
            <span class="required">*</span>
            <span asp-validation-for="Guardians[0].FirstName" class="text-danger"></span>

        </div>
    </div>
    <input type=submit value="Create" />
</form>

结果:

enter image description here