validation is not showing for nested model

Anjali Agarwal 1,591 Reputation points
2023-02-13T05:24:20.5266667+00:00

I have the following Model:

public partial class EmployeeInfo

{

public int EmployeeInfoId { get; set; }

[Required]

[DisplayName("Last Name")]

public string LastName { get; set; } =null!;

public virtual ICollection<EmergencyInfo> EmergencyInfos { get; } = new List<EmergencyInfo>();

}

I have another Model class for EmergencyInfo

public partial class EmergencyInfo

{

public int EmergencyInfoId { get; set; }

public int EmployeeInfoId { get; set; }

[DisplayName("First Name")]

[Required]

public string? FirstName { get; set; } = null!;

public virtual EmployeeInfo? EmployeeInfo { get; set; }

}

Although the Firstname is required in EmergencyInfo Model. When I click the submit button, it does not show that Firstname is required . It does show that LastName is required because that field exists in the EmployeeInfo model. below is my razor page:

@model AckPackage.Models.EmployeeInfo

@{

ViewData["Title"] = "Create";

}

<div class="form-group row">

            <div class="col-sm-4">

                <label asp-for="LastName" class="control-label"></label>

                <input asp-for="LastName" class="form-control input-lg" />

                <span asp-validation-for="LastName" class="text-danger"></span>

            </div>

    <div class="col">

                            <label asp-for="@Model.EmergencyInfos.ToList()[i].FirstName" class="control-label"></label>

                            <input name="EmergencyInfos[@i].FirstName" value="@(string.IsNullOrEmpty(Model.EmergencyInfos.ToList()[0].FirstName)? "": @Model.EmergencyInfos.ToList()[0].FirstName)" class="form-control" />

                 

<span asp-validation-for="@Model.EmergencyInfos.ToList()[0].FirstName" class="text-danger"></span>

                    </div>

@section Scripts {

@{

    await Html.RenderPartialAsync("_ValidationScriptsPartial");

}

Lastname not filled always displays LastName is required, but Firstname from emergencyInfo always goes to HTTPPost without showing validation error message.

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-02-14T07:39:12.71+00:00

    Hi @Anjali Agarwal

    You can modify your code as below:

    1. In the EmployeeInfo class, try to use List<T> instead of ICollection<T> to add the EmergencyInfos property.
           public class EmployeeInfo
           {
               public int EmployeeInfoId { get; set; }
               public string LastName { get; set; } = null!;
               public string EmailAddress { get; set; } = null!;
               public virtual List<EmergencyInfo> EmergencyInfos { get; set; } = new List<EmergencyInfo>();
           }
    
    1. In the view page. use the following code to display the properties:
       <div class="form-group">
           @if (Model != null && Model.EmergencyInfos != null && Model.EmergencyInfos.ToList().Count > 0)
           {
               var count = Model.EmergencyInfos.Count;
               @for (var i = 0; i < count; i++)
               {
                   <div class="form-group row">
                       <div class="col">
                           <label class="control-label">LastName</label>
                           <input asp-for="EmergencyInfos[i].LastName" value="@(string.IsNullOrEmpty(Model.EmergencyInfos.ToList()[i].LastName)? "": @Model.EmergencyInfos.ToList()[i].LastName)" class="form-control" />
                           <span asp-validation-for="EmergencyInfos[i].LastName" class="text-danger"></span>
                       </div>
                   </div> 
               }
           }
           else
           {
               <div class="form-group row">
                   <div class="col">
                       <label asp-for="EmergencyInfos[0].LastName" class="control-label">LastName</label>
                       <input asp-for="EmergencyInfos[0].LastName" class="form-control" />
                       <span asp-validation-for="EmergencyInfos[0].LastName" class="text-danger"></span>
                   </div> 
               </div>
           }
       </div>
    

    The result as below:

    image1


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.