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 issue here is that the line below is mixing server-side Razor/C# with client-side JavaScript, which won’t work:
@Model.car = JSON.parse(this.responseText);
@Model.car is evaluated on the server when the page is rendered, while JSON.parse(...) runs later in the browser after the AJAX request completes. By that time, the Razor code has already finished executing, so the browser cannot assign values back to the server-side model.
Instead, the approach is to parse the JSON response in JavaScript and then populate the input fields directly in the page (DOM).
For example:
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var car = JSON.parse(this.responseText);
document.querySelector("[name='car.Brand']").value = car.brand;
document.querySelector("[name='car.Year']").value = car.year;
document.querySelector("[name='car.Price']").value = car.price;
}
};
Your handler (OnGetDataResponse) is already returning the Car object as JSON correctly, so once you parse the response and assign the values to the inputs on the client side, the fields should populate as expected.
Also, please treat the snippet above as a general reference. Depending on how your page is structured (input names, selectors, or layout), you may need to adjust the selectors or logic slightly 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.