How to change value of input field in a strongly-typed view from controller

Jeddah 86 Reputation points
2023-01-27T11:07:27.1466667+00:00

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);
        }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,207 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,146 Reputation points
    2023-01-27T12:07:02.7466667+00:00

    Tag helpers read the ModelState. ModelState is a property of the controller that contains the submitted values and any validation errors. This is a standard MVC feature.

    One option is clearing the ModelState as shown below.

    [HttpPost]
    public IActionResult emp(Employee eee)
    {
        ModelState.Clear();
    
        eee.EmpID = "5555";
        eee.Name = "ABC";
        eee.Address = "XYZ";
        return View(eee);
    }
    

    Another option is creating a new model and populating the new model from the submitted values.

    Changing the user's submitted values is probably not the best idea and most likely there is better solution. Can you tell us the reasoning behind changing the submitted values?

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful