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:
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