How to update textbox value

前端 未结 1 1264
暗喜
暗喜 2021-01-24 11:59

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

相关标签:
1条回答
  • 2021-01-24 12:36

    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.

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