How to trigger error message when client-side validation on modal create in ASP.NET Core MVC

I have trouble in display basic error message despite the validation is working (as in preventing the item to be created with invalid value). Here are the code

This is my Views (Index.cshtml)

@model PickYaKoi.Models.KoiViewModel

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>

    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- Table here -->


<!-- Modal -->
<div class="modal fade" id="createKoiModal" tabindex="-1" aria-labelledby="createKoiModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="createForm" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="createKoiModalLabel">Create new Koi</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>

                <div class="border p-3 mt-4 modal-body">
                
                    <div class="mb-3">
                        <label asp-for="Koi.Name" for="Name" class="p-0">Name</label>
                        <input asp-for="Koi.Name" id="Name" type="text" name="Name" class="form-control" />
                        <span asp-validation-for="Koi.Name" class="text-danger"></span>
                    </div>
                    <div class="mb-3">
                        <label asp-for="Koi.Description" for="Description" class="p-0">Description</label>
                        <input asp-for="Koi.Description" id="Description" name="Description" type="text" class="form-control" />
                        <span asp-validation-for="Koi.Description" class="text-danger"></span>
                    </div>
                    <div class="mb-3">
                        <label asp-for="Koi.Amount" for="Amount" class="p-0">Amount Left</label>
                        <input asp-for="Koi.Amount" id="Amount" type="text" name="Amount" class="form-control" />
                        <span asp-validation-for="Koi.Amount" class="text-danger"></span>
                    </div>
                    <div class="mb-3">
                        <label asp-for="Koi.Status" for="Status" class="p-0">Choose Status</label>
                        <select asp-for="Koi.Status" id="Status" name="Status" class="form-control">
                            <option value="Available">Available</option>
                            <option value="Unavailable">Unavailable</option>
                        </select>
                    </div>
               
                        <div class="modal-footer">
                            <a href="#" class="btn btn-primary form-control" id="btnSave"> Create </a>
                            <button class="btn btn-outline-secondary form-control" id="btnCancel" data-bs-dismiss="modal">Go back</button>
                        </div>
                    
                </div>
            </form>
        </div>
    </div>
</div>


This is my models

public class Koi
{
    [Key]
    public int Id { get; set; }

    [Required]
    [DisplayName("Koi Name")]
    public string Name { get; set; }

    [DisplayName("Koi Description")]
    [MaxLength(255)]
    public string Description { get; set; }

    [Required]
    [Range(0, 9999)]
    public int Amount { get; set; }

    [Required]
    public availableStatus Status { get; set; }

}

I tried to call partial name=”_ValidationScriptsPartial” which help enable those client-side validation at the bottom of Index.cshtml. This usually work on a basic non-modal form; however, I am working with modal right now

@section Scripts {
    @{
        <partial name="_ValidationScriptsPartial" />
        
    }

}

In _ValidationScriptsPartial.cshtml has:

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

Edit: So after testing around, I might found the original problem that preventing the error message from appearing in the first place. By only using Koi object as only model, it’s able to display that message. However I want to use KoiViewModel, which contains both Koi and List object, for both Create Modal and Display Table. Once I put that as a Model for that View, The error messsage no longer display as usual.

2

You should add asp-action to the form and Your ask-for and asp-validation-for should write only the property. Your create button can write like this <input type="submit" value="Create" class="btn btn-primary" />

I copy your code and complete the Action to write an example .

1.KoiViewModel.cs. If the class name and file name are inconsistent ,I have an error reported.So I make them the same

public class KoiViewModel
{
    [Key]
    public int Id { get; set; }
 
    [Required]
    [DisplayName("Koi Name")]
    public string Name { get; set; }
 
    [DisplayName("Koi Description")]
    [MaxLength(255)]
    public string Description { get; set; }
 
    [Required]
    [Range(0, 9999)]
    public int Amount { get; set; }
}
  1. I use the scaffolding tool to produce Create, Read, Update, and Delete (CRUD) pages for the movie model.
    you can learn more from this website:https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-8.0&tabs=visual-studio

  2. write the Action. The first (HTTP GET) CreateModal action method displays the initial Create form. The second ([HttpPost]) version handles the form post.The second CreateModal method (The [HttpPost] version) calls ModelState.IsValid to
    check whether the createKoiModal has any validation errors. you can learn more from this website:https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/validation?view=aspnetcore-8.0

controller.cs:

public IActionResult CreateModal()
{
     return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateModal([Bind("Id,Name,Description,Amount")] KoiViewModel koiViewModel)
{
     if (ModelState.IsValid)
     {
         _context.Add(koiViewModel);
         await _context.SaveChangesAsync();
         return RedirectToAction(nameof(Index));
     }
     return View(koiViewModel);
}
  1. .cshtml

     @model ExceptionDemo.Models.KoiViewModel
         <!-- Bootstrap JS -->
         <script src=https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
    
         <!-- jQuery -->
         <script src=https://code.jquery.com/jquery-3.6.0.min.js></script>
    
     <!-- Table here -->
    
    
     <!-- Modal -->
    @*<div class="modal" id="createKoiModal" tabindex="-1" aria-labelledby="createKoiModalLabel" >
         <div class="modal-dialog">
             <div class="modal-content"> *@
               <form id="createForm" method="post" asp-action="CreateModal">
                     <div class="modal-header">
                         <h5 class="modal-title" id="createKoiModalLabel">Create new Koi</h5>
                         <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                     </div>
    
                     <div class="border p-3 mt-4 modal-body">
    
                         <div class="mb-3">
                             <label asp-for="Name" for="Name" class="p-0">Name</label>
                             <input asp-for="Name" id="Name" type="text" name="Name" class="form-control" />
                             <span asp-validation-for="Name" class="text-danger"></span>
                         </div>
                         <div class="mb-3">
                             <label asp-for="Description" for="Description" class="p-0">Description</label>
                             <input asp-for="Description" id="Description" name="Description" type="text" class="form-control" />
                             <span asp-validation-for="Description" class="text-danger"></span>
                         </div>
    
    
                             <div class="modal-footer">
                                 <input type="submit" value="Create" class="btn btn-primary" />
                                 @* <a href="#" class="btn btn-primary form-control" id="btnSave"> Create </a> *@
                                 <button class="btn btn-outline-secondary form-control" id="btnCancel" data-bs-dismiss="modal">Go back</button>
                             </div>
    
                     </div>
                 </form>
            </div>
         @*</div>
     </div> *@
    

The result is :

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật