Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 516087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:41:44+00:00 2026-05-13T07:41:44+00:00

This behavior is making me wonder about my sanity.. I have a form that

  • 0

This behavior is making me wonder about my sanity..

I have a form that has two places that accept input, let’s call them ValueA and ValueB. The user can enter a value in either one and the form submits.

<div id="MyUpdateTarget">
 <% using (Ajax.BeginForm("MyControllerAction", new AjaxOptions { UpdateTargetId = "MyUpdateTarget" })) { %>
  <%=Html.TextBox("ValueA", Model.ValueA, new Dictionary<string, object> {
                                                    { "onchange", "$('#SubmitButton').click(); return false;" },
       }) %>
  <%=Html.TextBox("ValueB", Model.ValueB, new Dictionary<string, object> {
                                                    { "onchange", "$('#SubmitButton').click(); return false;" },
       }) %>
  <input id="SubmitButton" type="submit" value="Save" style="display: none;" />
 <% } %>
</div>

The Controller Action looks like this:

public ActionResult MyControllerAction(MyViewModel viewModel)
{

// do some other stuff …

 return PartialView("MyPartialView", viewModel);
}

The ViewModel is simply this:

public class MyViewModel
{
 private int _valueA;
 private int _valueB;

 public int ValueA 
 { 
  get
  {
   return _valueA;
  }
  set
  {
   if (value > 0)
   {
    ValueB = 0;
   }
   _valueA = value;
  } 
 }
 public int ValueB 
 {
  get
  {
   return _valueB;
  }
  set
  {
   if (value > 0)
   {
    ValueA = 0;
   }
   _valueB = value;
  }
 }
}

Now, the unexpected piece. Say the page initially loads and ValueB has a value of 7. The user changes ValueA to 5 and the form submits. I can put a breakpoint in the controller action and see both values in the viewModel parameter. At this point, ValueA is 5 and ValueB is 0 (due to the setting of ValueA). The action returns the viewModel as part of the PartialView. Back in the partial, I can put a breakpoint on the Html.TextBox(“ValueB”, Model.ValueB, …) line and see that ValueB is indeed 0. But when the form renders to the browser, ValueB still has a value of 7. And this is where I am stuck. I have even changed the Update target to a different div, so that the partial just spits out the form someplace completely different, but it still has the original value of 7, even though I saw through debugging that the value was 0 coming back from the controller.

Is there something I am missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:41:44+00:00Added an answer on May 13, 2026 at 7:41 am

    Here is the code from the MVC Source for the textbox:

         string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string));
                    tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : valueParameter**), isExplicitValue);
                    break;
    

    And the Code for GetModelStateValue()

        internal object GetModelStateValue(string key, Type destinationType) {
            ModelState modelState;
            if (ViewData.ModelState.TryGetValue(key, out modelState)) {
                if (modelState.Value != null) {
                    return modelState.Value.ConvertTo(destinationType, null /* culture */);
                }
            }
            return null;
        }
    

    So what happens is the Html "Helper" looks for the text box value, by matching the name, in your ViewData.ModalState, if its in the ModelState dictionary, it completely ignores the value you provided.

    So all that if (value > 0) { ValueA = 0; } doesn’t matter because its going to use the posted values in ModelState if the names match.

    The way I’ve fixed this is to blow away the ModelState before the view renders for certain values that I want to mess with in my view models. This is some code I’ve used:

        public static void SanitizeWildcards( Controller controller, params string[] filterStrings )
        {
            foreach( var filterString in filterStrings )
            {
                var modelState = controller.ModelState;
    
                ModelState modelStateValue;
                if( modelState.TryGetValue(filterString,out 
                        controller.ModelState.SetModelValue(filterString, new ValueProviderResult("","", null));
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making an app and seen that twitter has this style for navigating through
I am getting this strange behavior when making a Facetime call. The number is
I would like to have this behavior in a task history stack: First task
I have searched but could not find the reason for this behavior. I have
I currently have an immutable type called Gene , that only has 2 fields:
Ok this is really making me crazy and I need some help. I have
We have a hosted site that has a CMS we built running on a
I wonder what is the rationale behind making std::list<>::splice to invalidate the iterators that
I don't understand this behavior in javascript for inheritance I've always seen it defined
I've had this behavior happen to me twice recently and I was wondering what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.