A set of technologies in the .NET Framework for building web applications and XML web services.
Hi @Pablo The Tiger ,
Thanks for reaching out.
The thing to be aware of is that asp-for only works when Razor renders the HTML on the server. In your current setup, the modal is already rendered first, and then the car data arrives later via AJAX, so those inputs won’t be populated automatically.
Since you’re already returning a Partial View from OnGetDataResponse, the appropriate approach is to let the server render the form inside that Partial View, inject the HTML into the modal, and then post the edited values back using standard Razor Pages model binding.
You might arrange it like this:
- Put the form in
_CarPartial.cshtml:
Make the partial strongly typed and render the fields there. Place _CarPartial.cshtml in the same folder as Ajax.cshtml, or under Pages/Shared so Razor can locate it.
@model RazorPages.Pages.Car
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Edit car</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="post" asp-page-handler="SaveCar">
@Html.AntiForgeryToken()
<div class="modal-body">
<input asp-for="Id" type="hidden" />
<div class="mb-3">
<label asp-for="CarType" class="col-form-label"></label>
<input asp-for="CarType" class="form-control" />
<span asp-validation-for="CarType" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="Year" class="col-form-label"></label>
<input asp-for="Year" class="form-control" />
<span asp-validation-for="Year" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="Model" class="col-form-label"></label>
<input asp-for="Model" class="form-control" />
<span asp-validation-for="Model" class="text-danger"></span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
- Keep the modal as a container in
Ajax.cshtml:
Remove the hardcoded inputs and leave a placeholder for the partial. Accessibility attributes are included.
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" id="grid">
<!-- Partial will be injected here -->
</div>
</div>
</div>
- Load the partial into the modal and then show it:
Update your script to fetch the partial HTML and inject it:
document.getElementById("load").addEventListener("click", async () => {
const url = '@Url.Page("Ajax", "DataResponse")' + '?id=1';
const response = await fetch(url);
if (response.ok) {
const html = await response.text();
document.getElementById("grid").innerHTML = html;
const modal = new bootstrap.Modal(document.getElementById("exampleModal"));
modal.show();
}
});
- Handle the post in the PageModel:
To ensure the field names bind correctly, accept the Car object directly as a handler parameter:
public IActionResult OnPostSaveCar(Car car)
{
if (!ModelState.IsValid)
{
// Note: returning Page() will close the modal.
// For production, consider reopening the modal or using AJAX to submit and show validation errors.
return Page();
}
// Save car.Id, car.CarType, car.Year, car.Model here
return RedirectToPage();
}
Note: The code snippets above are simplified examples for illustration. You may need to adjust model names, namespaces, handlers, or validation setup to fit your project.
If you found my response helpful or informative in any way, I would greatly appreciate it if you could follow this guidance provide feedback.
Thank you.