TextBoxFor value not updating after post

后端 未结 5 728
暗喜
暗喜 2021-02-05 11:00

I have a simple strongly typed view, but I cant seem to update a textbox on my form after a post.

Here is my model:

public class Repair
  {
    public          


        
5条回答
  •  醉梦人生
    2021-02-05 11:26

    From my dealings with the issue, I feel that this is by design bug in the framework. IMO:

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

    should NOT be taking the value from ModelState but rather directly from the model. At least this would be my expectation when I alter the model and return View(model).

    ModelState.Clear() 
    

    is not an answer because it sanitizes ModelState erasing ValidationSummary. Removing a key from ModelState is neither good because it removes ValidationSummary for that key.

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

    is correct but just too arcane. Thus, in such cases, my preference would be to use:

    
    

    instead of

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

提交回复
热议问题