Search code examples
c#asp.net-core-mvcredirecttoaction

The view content doesn't refresh when using RedirectToAction method


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()
    {
        //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:

<!--
@{
    ViewData["Title"] = "Home Page";
}
@model WebApplication6.Controllers.TestViewModel

<div class="main">
    <form method="get">
        <label name="id" asp-for="Id">@Model.Id</label>
        <label asp-for="Name">@Model.Name</label>
        <input name="desc" type="text" value="@Model.desc" />
        <input name="t1" type="button" onclick="executeInsert()"/>
    </form>
</div>

<script type="text/javascript">
    function executeInsert() {
        $.get('/Home/Update');
    }
</script> -->

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.


Solution

  • This function cannot be invoked this way$.get('/Home/Update'); What you are trying to achieve can be done this way more eligantly you could try this sample:

    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:

    enter image description here

    I hope it would help you.