Hello
I am using ASP.NET Core 3.1
How can I change the value of the input fields in a strongly-typed view from controller. I have this simple example. I have three input fields and when the usee enters some values and submit them, I modify the values the object
eee.EmpID = "5555";
eee.Name = "ABC";
eee.Address = "XYZ";
and return it back to the view. BUT I found the fields values have not changed and they have still the same user values.
How can I change the value displayed in the input fields in the view from controller?
what is missing?
Sample code:
public class Employee
{
public string EmpID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
<form asp-action="emp" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="EmpID" class="control-label"></label>
<input asp-for="EmpID" class="form-control" />
<span asp-validation-for="EmpID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address" class="control-label"></label>
<input asp-for="Address" class="form-control" />
<span asp-validation-for="Address" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
[HttpGet]
public IActionResult emp()
{
return View();
}
[HttpPost]
public IActionResult emp(Employee eee)
{
eee.EmpID = "5555";
eee.Name = "ABC";
eee.Address = "XYZ";
return View(eee);
}