Share via

How to produce bootstrap modal editable form from html data object list in Razor Pages

Pablo The Tiger 20 Reputation points
2026-04-06T00:54:32.4433333+00:00

Hello,

Lately I was receiving valuable help by @Jack Dang (WICLOUD CORPORATION) and @SurferOnWww on how to show bootstrap modal form and client side data validation.

Now I would like to be able to show the form when I click the Edit button of each row from the html list (the .cshtml file) and pass the corresponding id. I tried the following and the JS doesn´t even execute.

Clarification: the issue of my question before was successfuly solved.

forum 30

Thanks! I appreciate your help very much.

Pablo

Developer technologies | ASP.NET | ASP.NET Core

Answer accepted by question author
  1. SurferOnWww 5,921 Reputation points
    2026-04-06T01:40:57.11+00:00

    Now I would like to be able to show the form when I click the Edit button of each row from the html list (the .cshtml file) and pass the corresponding id.

    Shown below is sample. Be aware that there will be lot of other troubles even when the case-sensitive issue of JavaScript method has been solved according to the AI answer. Therefore, I suggest that you follow the completely working sample shown below.

    Model

    enter image description here

    .cshtml.cs

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using RazorPages.Models;
    
    namespace RazorPages.Pages
    {
        public class Ajax3Model : PageModel
        {
            public IList<Car> Cars { get; set; } = default!;
            public Car Car { get; set; } = default!;
    
            public void OnGet()
            {
                Cars = new List<Car>
                {
                    new Car { Id = 1, Brand = "Ford", Year = 2022, Price = 20000 },
                    new Car { Id = 2, Brand = "Renault", Year = 2019, Price = 25000 },
                    new Car { Id = 3, Brand = "Audi", Year = 2025, Price = 40000 }
                };
            }
    
            public IActionResult OnGetDataResponse(int? id)
            {
                if (id == null)
                {
                    return NotFound();
                }
    
                var cars = new List<Car>
                {
                    new Car { Id = 1, Brand = "Ford", Year = 2022, Price = 20000 },
                    new Car { Id = 2, Brand = "Renault", Year = 2019, Price = 25000 },
                    new Car { Id = 3, Brand = "Audi", Year = 2025, Price = 40000 }
                };
    
                var car = cars.FirstOrDefault(c => c.Id == id);
                if (car == null)
                {
                    return NotFound();
                }
    
                return new JsonResult(car);
            }
    
            public IActionResult OnPostEdit(Car car)
            {
                if (!ModelState.IsValid)
                {
                    Cars = new List<Car>
                    {
                        new Car { Id = 1, Brand = "Ford", Year = 2022, Price = 20000 },
                        new Car { Id = 2, Brand = "Renault", Year = 2019, Price = 25000 },
                        new Car { Id = 3, Brand = "Audi", Year = 2025, Price = 40000 }
                    };
                    
                    // The ModelState is invalid, so we return the page with the validation errors.
                    // ViewData is used to pass the validation result to the view, which can then
                    // display appropriate messages to the user in Modal.
                    ViewData["ValidationResult"] = "invalid";
                    return Page();
                }
                return RedirectToPage("Index");
            }
        }
    }
    

    .cshtml

    @page
    @model RazorPages.Pages.Ajax3Model
    
    <!-- Bootstrap Modal -->
    <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static"
         data-bs-keyboard="false" tabindex="-1"
         aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h1 class="modal-title fs-5"
                        id="staticBackdropLabel">
                        Edit Car
                    </h1>
                    <button type="button" class="btn-close"
                            data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form method="post" id="myForm" asp-page-handler="Edit">
                        <div class="form-group">
                            <label asp-for="Car.Id" class="control-label"></label>
                            <input type="text" asp-for="Car.Id" class="form-control" />
                            <span asp-validation-for="Car.Id" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label asp-for="Car.Brand" class="control-label"></label>
                            <input type="text" asp-for="Car.Brand" class="form-control" />
                            <span asp-validation-for="Car.Brand" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label asp-for="Car.Year" class="control-label"></label>
                            <input type="text" asp-for="Car.Year" class="form-control" />
                            <span asp-validation-for="Car.Year" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label asp-for="Car.Price" class="control-label"></label>
                            <input type="text" asp-for="Car.Price" class="form-control" />
                            <span asp-validation-for="Car.Price" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <input type="submit" value="Submit" class="btn btn-primary" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    

    enter image description here

    enter image description here

    Initial

    enter image description here

    Edit link clicked

    enter image description here

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 16,115 Reputation points Microsoft External Staff Moderator
    2026-04-06T02:51:40.8133333+00:00

    Hi @Pablo The Tiger ,

    Thanks for the update.

    From the code in your screenshot, the reason the JavaScript does not execute is that loadCar(id) uses await fetch(...), but the function itself is not marked as async.

    In other words, this:

    function loadCar(id) {
        const response = await fetch(url);
    }
    

    is invalid JavaScript, so the script fails to parse and the click handler never runs.

    Please change it to:

    async function loadCar(id) {
        const url = '@Url.Page("Ajax", "GetCar")' + '&id=' + id;
    
        const response = await fetch(url);
        if (response.ok) {
            const html = await response.text();
            $("#grid").html(html);
    
            const form = $("#carForm");
            form.removeData("validator");
            form.removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse(form);
    
            const modal = new bootstrap.Modal(document.getElementById("exampleModal"));
            modal.show();
        }
    }
    

    And your button can call it like this:

    <button type="button" class="btn btn-primary btn-sm" onclick="loadCar(@car.Id)">Edit</button>
    

    Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.


  2. Q&A Assist
    2026-04-06T01:00:52.2866667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The JavaScript handler is not firing because the function name used in the onclick attribute does not match the function name defined in the script. JavaScript is case-sensitive, so loadCar and LoadCar are treated as different identifiers.

    In the Razor markup, the button calls:

    <button type="button" class="btn btn-primary btn-sm" onclick="loadCar(@car.Id)">
        <i class="bi bi-pencil"></i>Edit
    </button>
    

    But the script defines:

    function LoadCar(id) {
        alert("AAAAAAAAAAAAAAAAAAAA");
        // ...
    }
    

    To make the click event invoke the function, use the same casing in both places. For example, change the button to call LoadCar:

    <button type="button" class="btn btn-primary btn-sm" onclick="LoadCar(@car.Id)">
        <i class="bi bi-pencil"></i>Edit
    </button>
    

    and keep the function as:

    <script>
        async function LoadCar(id) {
            alert("AAAAAAAAAAAAAAAAAAAA");
            const url = '@Url.Page("Ajax", "GetCar")' + '&id=' + id;
            const response = await fetch(url);
            if (response.ok) {
                const html = await response.text();
                $("#grid").html(html);
    
                const form = $("#carForm");
                form.removeData("validator");
                form.removeData("unobtrusiveValidation");
                $.validator.unobtrusive.parse(form);
    
                const modal = new bootstrap.Modal(document.getElementById("exampleModal"));
                modal.show();
            }
        }
    </script>
    

    After this change, clicking Edit on any row will call LoadCar with the corresponding car.Id, execute the alert, and then run the code that loads and shows the Bootstrap modal with the editable form.


    References:

    0 comments No comments

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.