Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I created an update page for ASP.NET Core MVC (.NET 7). I want that after user completes the form for update and clicks on Submit, the current page refreshes with new data. The page is refreshed, but the data is not. However, when I click on Ctrl+F5 or F5, the new data shows it too. Why does this happen and how can I fix this?

What I have tried:

This is the part of my code in controller for refresh page with RedirectToAction:

C#
if(result != 0)
                 {
                     return RedirectToAction("Update", new { name = productModel.titleENG });
                 }
                 else
                 {
                     ModelState.AddModelError("", "try again later");
                     return View("UpdateProduct", new { name = productModel.titleENG });
                 }

and this is my JavaScript:
JavaScript
function ManagmentProduct(event) {
    const form = document.getElementById('ManageProduct');
    let confirmAction = confirm("are you sure about update?");
    if (confirmAction) {
        event.preventDefault(); // prevent the default form submission
        const formData = new FormData(form); // create a new form data object
        const farsiText = document.getElementById('definitionFA').innerHTML; // get the contenteditable value
        formData.append('definitionFA', farsiText); // add the contenteditable value to the form data
        const EnglishText = document.getElementById('definitionENG').innerHTML; // get the contenteditable value
        formData.append('definitionENG', EnglishText); // add the contenteditable value to the form data
        fetch(form.action, {
            method: 'POST',
            body: formData
        })
            .then(location.reload(true))
            .then(response => response.json())
            .then(data => console.log(data))
            .catch(error => console.error(error)); 
    } else {
        event.preventDefault();
        return false;
    }
}
Posted
Updated 18-Aug-23 7:18am
v4

1 solution

I find solution:
First I use
return RedirectToAction("Update", "Product", new { name = productModel.titleENG });

instead of
return View("UpdateProduct", new { name = productModel.titleENG });

then in JavaScript
I use this:
fetch(form.action, {
            method: 'POST',
            body: formData
        })
            .then(response => {
                if (response.ok) {
                    location.reload({ forceReload: true }); // reload the page
                } else {
                    console.error('Form submission failed.');
                }
            })
            .catch(error => console.error(error)); 

so the page is refresh with new data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900