How to clear textboxes defined with MVC HTML helpers

前端 未结 2 1572
庸人自扰
庸人自扰 2021-01-06 09:46

I can\'t figure out how to do this very simple thing: My page contains a set of textboxes that a user can fill out to add an item to a list. Then the item shows up in a drop

相关标签:
2条回答
  • 2021-01-06 09:59

    This is working for me on an MVC3 site log on page.

    ModelState.Clear();
    model.UserName = string.Empty;
    model.Password = string.Empty;
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    

    This will clear the login textboxes used for password and username, and keep any model errors.

    0 讨论(0)
  • 2021-01-06 10:04

    The HTMLHelper's first look at the ModelState and ViewData to see if any values match their key and then finally use whatever value you provide them.

    If you need to reset the textboxe's value you also need to clear the ModelState entry with the matching key. Another alternative is redirecting to the same page instead of simply rendering a view via javascript or with MVC.

    0 讨论(0)
提交回复
热议问题