How does asp.net MVC remember my incorrect values on postback?

后端 未结 3 1282
忘了有多久
忘了有多久 2021-01-05 15:27

This is working, but how???

I have a controller action for a post:

[AcceptVerbs(HttpVerbs.Post )]
public ActionResult Edit(Person person)
{
   bool i         


        
3条回答
  •  醉话见心
    2021-01-05 16:15

    I had some code in the 'postback' that would remove invalid promo-codes and capitalize the response. Even if you update the model with the new value it won't be displayed because the ModelState value takes precedence (as others have already answered).

      <%= Html.TextBox("PromoCode", Model.PromoCodes) %>
    

    But if you have a case where this was happening and you don't want the old value persisted you need to do this:

    ModelState.Remove("PromoCode"); 
    

    or explicitly set the new value into modelstate (probably the better approach) :

    ModelState.SetModelValue("PromoCode", 
          new ValueProviderResult(newValue, newValue, CultureInfo.CurrentCulture));
    

提交回复
热议问题