I have a textbox in my View. I input a number into the textbox, and then i want the controller to multiply the number and put the result into the textbox.
How can I do t
Try
[HttpPost]
public ActionResult Index(int number)
{
number = number * 2;
ViewData["id"] = number;
ModelState.Clear(); // this is the key, you could also just clear ModelState for the id field
return View(ViewData);
}
You can also just use a regular html input instead of the HtmlHelper and your code would work as expected.
The default behavior of the Html helper is biting you. It looks for data in ModelState collection before using what is in ViewData. The reasoning is the normal case is a POST > Validation Fail > Return View, so showing the data the user entered is the intended behavior.