TextBoxFor value is not updated after publication

I have a simple strongly typed view, but I cannot update the text box in my form after posting.

Here is my model:

public class Repair { public string Number { get; set; } } 

And in my opinion this is a TextBox:

  @Html.TextBoxFor(x => x.Number) 

I am trying to update a text box after a message on my controller:

  [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(Repair r) { r.Number = "New Value"; return View(r); } 

Even if I set Number to a new value, the text in the text box does not change. What am I doing wrong?

+4
source share
5 answers

Use ModelState.Clear() before setting the value

 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(Repair r) { ModelState.Clear(); //Added here r.Number = "New Value"; return View(r); } 
+17
source

When you send the model back to ActionResult and return the same view, the values ​​for the model objects are contained in the ModelState. ModelState is what contains valid / invalid field information as well as actual POSTED values. If you want to update the model value, you can do one of two things:

ModelState.Clear()

or

ModelState["Number"].Value = new ValueProviderResult("New Value", "New Value", CultureInfo.CurrentCulture)

+8
source

From my relationship with the problem, I feel that this is a design error within the framework. IMO:

 @Html.TextBoxFor(x => x.Number) 

You should NOT take a value from ModelState , but rather directly from the model. At least it will be my expectation when I change the model and return View(model) .

 ModelState.Clear() 

is not an answer because it ModelState erasing ValidationSummary. Removing a key from ModelState not good because it removes the ValidationSummary for this key.

 ModelState["Number"].Value = new ValueProviderResult("New Value", "New Value", CultureInfo.CurrentCulture) 

is correct but too secret. Thus, in such cases, I prefer to use:

 <input type="text" name="Number" id="Number" value="@Model.Number"/> 

instead

 @Html.TextBoxFor(x => x.Number) 
+4
source

If you find that ModelState.Clear () is too destructive, you can only target the element that you are changing, preserving the rest with ModelState.Remove ()

 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(Repair r) { r.Number = "New Value"; ModelState.Remove("Number"); return View(r); } 

Also, it doesn't seem to matter with Remove () or Clear (), whether you call this method before updating the model or after

+2
source

Try it. You can put it in the base controller if you want. This works well for me. This makes it so unobtrusive that it still works, but the values ​​from the model are displayed correctly, as EXPECTED.

 public class BaseController : Controller { public override void OnActionExecuted(ActionExecutedContext context) { ModelState.ToList().Select(x => x.Value).ToList().ForEach(x => { x.AttemptedValue = null; x.RawValue = null; }); // Do a bunch of stuff here if needed. Stuff like validation. base.OnActionExecuted(context); } } 
0
source

Source: https://habr.com/ru/post/1499194/


All Articles