0

Below are the Baseclass and Userclass model i have used and i have also added required annotations in both the Base and Derived class . I have used this class in asp.net core razor view page and implemented jquery-validation for this . I could see validation happening only for derived class but not base class . How can i fix this ?

 public class BaseModel
    {
        [Required]
        public string TicketNo { get; set; }
    }

 public class UserRoleModel : BaseModel
    {
        [Required]
        public string UserID { get; set; }
        [Required]
        public string RoleName { get; set; }
        [Required]
        public string AddDeleteFlag { get; set; }
    }

And my cshtml page


 <div class="form-group">
                        <label for="TicketNo">Ticket Number</label>
                        <input type="text" name="Employee" asp-for="TicketNo" autocomplete="off" class="form-control input-sm" placeholder="Ticket Number">
                        <span asp-validation-for="TicketNo" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label for="UserID">User ID</label>
                        <input type="text" name="UserID" asp-for="UserID" autocomplete="off" class="form-control input-sm" placeholder="User ID">
                        <span asp-validation-for="UserID" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label for="RoleName">Role Name</label>
                        <input type="text" name="RoleName" asp-for="RoleName" autocomplete="off" class="form-control input-sm" placeholder="Role Name">
                        <span asp-validation-for="RoleName" class="text-danger"></span>
                    </div>
                    <div>
                        <input type="submit" value="AddRole" asp-for="AddDeleteFlag" class="btn btn-primary btn-block">
                        <input type="submit" value="DeleteRole" asp-for="AddDeleteFlag" class="btn btn-primary btn-block">
                    </div>

i have also added required js reference

 <script src="~/lib/jquery-validate/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

enter image description here

Now Validation happening only for derived class not base class !

1 Answer 1

1

You enter the wrong name for TicketNo property , please modify the markup to below :

<div class="form-group">
    <label for="TicketNo">Ticket Number</label>
    <input type="text" name="TicketNo" asp-for="TicketNo" autocomplete="off" class="form-control input-sm" placeholder="Ticket Number">
    <span asp-validation-for="TicketNo" class="text-danger"></span>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.