The view doesn't refresh when using RedirectToAction method

Xie Steven 811 Reputation points
2021-08-27T02:16:52.39+00:00

Hi,

I create a simple asp.net core mvc project.

The HomeController.cs looks like the following:

public class HomeController : Controller  
    {  
        public HomeController()  
        {  
        }  
  
        public IActionResult Index(string name, string desc)  
        {  
            Random random = new Random();  
            TestViewModel viewModel = new TestViewModel();  
            viewModel.Id = random.Next(0,7).ToString();  
            viewModel.Name = name;  
            viewModel.desc = desc;  
            return View(viewModel);  
        }  
  
        public IActionResult Update(TestViewModel model)  
        {  
            //return View("Index", new TestViewModel() { Name="Steven", desc="Hello" });  
            return RedirectToAction("Index", new { name = "Steven", desc="Hello" });  
        }  
    }  

The ViewModel class:

public class TestViewModel  
    {  
        public string Name { get; set; }  
        public string Id { get; set; }  
        public string desc { get; set; }  
    }  

The Razor page:

126954-2021-08-27-101542.jpg

When I click the button, I actually has send the get request and the it has redirected to index action, but the browser doesn't refresh the view content.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,225 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Farid Uddin Kiron MSFT 456 Reputation points Microsoft Vendor
    2021-08-27T06:41:40.257+00:00

    Hello @Xie Steven

    $.get('/Home/Update'); This function will not invoked the Update method.

    What you are trying to achieve can be done this way more eligantly you could try:

    Controller Loading View:

     public IActionResult Index(string name, string desc)  
            {  
                Random random = new Random();  
                TestViewModel viewModel = new TestViewModel();  
                viewModel.Id = random.Next(0, 7).ToString();  
                viewModel.Name = name;  
                viewModel.desc = desc;  
                return View(viewModel);  
            }  
    

    Controller Submit New Value:

            [HttpPost]  
            public IActionResult Update(TestViewModel model)  
            {  
              return RedirectToAction("Index", new { name = model.Name, desc = model.desc });  
            }  
    

    View:

    @model MVCApps.Models.TestViewModel  
    
    @{  
        ViewData["Title"] = "ViewContentTestIndex";  
    }  
    
    <h3>View Content Test Index</h3>  
    <hr />  
    
    @using (Html.BeginForm("Update", "UserLog"))  
    {  
    
        <div>  
            Id: <label name="id" asp-for="Id">@Model.Id</label>  
            Name:<label asp-for="Name">@Model.Name</label>  
            Desc:<label asp-for="desc">@Model.desc</label>  
        </div>  
    
        <hr />  
        <div>  
    
            <label asp-for="Name">Name</label>  
            <input name="Name" type="text" value="@Model.Name" />  
            <label asp-for="Name">Desc</label>  
            <input name="desc" type="text" value="@Model.desc" />  
            <input id="Button" type="submit" value="Save" class="btn btn-primary" />  
        </div>  
    
    
    }  
    

    Output:

    126940-refreshviw.gif

    I hope it would help you.

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Md Farid Uddin Kiron

    0 comments No comments

  2. Bruce (SqlWork.com) 57,481 Reputation points
    2021-08-27T15:43:45.307+00:00

    There are two issues with your code,

    • As suggested above, the ajax call should be a post, and the form data be passed.
    • the response html is passed to the the ajax callback, and you need JavaScript code to display it.

    As you want to redirect on the completion of the ajax call, not sure why you use ajax

    0 comments No comments