asp.net-mvcdtomodel-validation

How to assign a nested property of an entity class for an asp-for tag for model validation?


I have an entity class like this:

public class User : BaseEntity
    {
       
        public long id { get; set; }

        [Required(ErrorMessage ="It is a mandatory field.")]
        public string fullname { get; set; }

    }

And a DTO class: [NOTE there is a **list **of Users]

public class ResultGetUsersDto
    {
        public List<GetUsersDto> Users { get; set; }
        public int Rows { get; set; }
    }

And then, I have a razor page in which user's information is going to be change by a modal: HEADER:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using alefbafilms.application.Services.Users.Queries.GetUsers
@model ResultGetUsersDto;

BODY:

<!-- Modal -->
    <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">

            <form id="formdata" asp-action="UpdateUser" method="post">

            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myModalLabel">UPDATE USERS</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="txtEditIdUser" />
                    <input name="" asp-for="" type="text" class="form-control" id="txtEditFullname" />
   <button type="button" onclick="UpdateUser()" class="btn btn-primary waves-effect waves-light">Save</button>
                </div>

            </div><!-- /.modal-content -->

            </form>

        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

The problem is, I can't access nested-property of List class. I mean "Users" and "fullname" property. How can I access it for setting into "asp-for" for model validation?

I tried to discover nested property of ResultGetUsersDto class, such as:

  1. asp-for="Users.fullname"
  2. asp-for="fullname"
  3. asp-for="Users[0].fullname"
  4. ... but I failed.

Solution

  • I have just found the answer! As a matter of fact, there was a simple and ridiculous answer for that. The answer is that it has nothing to do with how many DTOs (request or response kinds) we have! we must set the same User validations as DTO validations. Thus, we will have:

        public class GetUsersDto
    {
       public long id { get; set; }
    
        [Required(ErrorMessage ="It is a mandatory field.")]
        public string fullname { get; set; }
    }
    

    and about using this: HEADER: (the same code):

    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    

    @using alefbafilms.application.Services.Users.Queries.GetUsers @model ResultGetUsersDto;

    BODY:

                            <input asp-for="Users[0].fullname"
                           type="text"
                           class="form-control"
                           id="txtEditFullname" />
                        <span asp-validation-for="Users[0].fullname"
                          class="text-danger"
                          data-valmsg-for="Users[0].fullname">
                        </span>
    

    We have to use our first index (zero index) as a sample of our entity.