lundi 5 septembre 2016

How is the input elements value persists in MVC after a postback?

i am new to MVC and i have a question in mind. Http is a stateless protocol and we maintain state in asp.net using State Management technique. My question is how does MVC handles to persist the value of an element after a postback. Example: I have a controller like this

public class LoginController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Validate(Cred obj)
        {
            if (obj.UserName == "A" && obj.Password == "A")
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View("Index");
            }
        }
    }

and a view like this

 @using (Html.BeginForm("Validate", "Login"))
        {
            <fieldset>
                <div>
                    UserName:@Html.TextBox("userName")
                </div>
            </fieldset>
            <fieldset>
                <div>
                    Password:@Html.TextBox("password")
                </div>
            </fieldset>
            <input type="submit" value="Submit" />
        }

whenever my page fails to get validate i redirect to my index page and the value in the textbox remains unchanged. How is this managed in MVC. Thank you

Aucun commentaire:

Enregistrer un commentaire