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.
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:
Controller Submit New Value:
View:
Output:
I hope it would help you.