0

In my ASP MVC web application. I have this controller

namespace Test.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index(UserDetails user)
        {
            user.UserEmail = "Email one";
            return View(user);
        }

        // Post: Home
        [HttpPost]
        public ActionResult Index(UserDetails user, string command)
        {
            user.UserEmail = "Email two";
            return View(user);
        }

        #region Helpers
        #endregion
    }
}

and this view

@model Test.Models.UserDetails

@{    Layout = null;}

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{                    
@Html.DisplayNameFor(model => model.UserEmail)
       @Html.EditorFor(model => model.UserEmail)
       <input type="submit" name="Command" value="Search" class="btn btn-default" />
A}
    </body>
</html>

When I first run the application it will show “Email one” in the textbox. But when I hit the submit button it will not change the value in the textbox to “Email two”. What I have to change on my code?

Stefan
  • 555
  • 5
  • 18
  • Probably it is calling the first method and so never going to the second method at all. Do you need to pass user object in your first method when page first loads? If not there can be the solution – Pawan Nogariya Jun 03 '16 at 21:23
  • 1
    The behavior when your seeing is by design and is described in [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111). The correct approach is to follow the PGG pattern, but an option is to use `ModelState.Clear()`. You should also remove the `UserDetails user` parameter in the GET method (and change it to `string userEmail` if you want to pass it around) and initialize the object in the method (a GET method should generally not contain parameters which are models) –  Jun 03 '16 at 21:52

0 Answers0