Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - How to change the value of a textbox in a post?

Tags:

asp.net-mvc

After a user clicks the submit button of my page, there is a textbox that is validated, and if it's invalid, I show an error message using the ModelState.AddModelError method. And I need to replace the value of this textbox and show the page with the error messages back to the user.

The problem is that I can't change the value of the textbox, I'm trying to do ViewData["textbox"] = "new value"; but it is ignored...

How can I do this?

thanks

like image 910
Paulo Avatar asked Dec 18 '22 08:12

Paulo


1 Answers

You can use ModelState.Remove(nameOfProperty) like:

ModelState.Remove("CustomerId");
model.CustomerId = 123;
return View(model);

This will work.

like image 199
Jeferson Tenorio Avatar answered Jan 14 '23 01:01

Jeferson Tenorio