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 8932039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:15:04+00:00 2026-06-15T09:15:04+00:00

I have an issue which I really just don’t understand. I have a very

  • 0

I have an issue which I really just don’t understand. I have a very simple model which has a List as a public member. Whenever my controller removes an element from the model on postback the TextBoxFor() HTML helpers do not seem to pick up the changes. These helpers appear to be caching something but I cannot put my finger on it.

A Demo/Repro can be found here: http://broken.azurewebsites.net

Repro

  1. Navigate to http://broken.azurewebsites.net
  2. Notice the 4 column values populated with zero based values
  3. Hit the “test” button to POST back the page where I remove the first item in the list
  4. Notice The “real” values are correct and the 0 element has been removed, however the problem here is with the values rendered via TextBoxFor(). I cannot figure out why it is rendering 0 still when that element no longer exists.

Models

public class ItemViewModel
{
    public string Description { get; set; }
    public decimal? Amount { get; set; }
}

public class TestViewModel
{
    public TestViewModel()
    {
        Items = new List<ItemViewModel>();
    }

    public List<ItemViewModel> Items { get; set; }
}

Controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new TestViewModel();

        for (var i = 0; i < 4; i++)
        {
            model.Items.Add(new ItemViewModel { Description = i.ToString(), Amount = i });
        }

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(TestViewModel model)
    {
        model.Items.RemoveAt(0);

        return View(model);
    }

}

View

@model Demo.Models.TestViewModel
@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr><td>Description</td><td>Amount</td><td>Real-Description</td><td>Real-Amount</td></tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.Items.Count; i++)
            {
                var ii = i;
                <tr>
                    <td>@Html.TextBoxFor(m => m.Items[ii].Description)</td>
                    <td>@Html.TextBoxFor(m => m.Items[ii].Amount)</td>
                    <td>@Model.Items[ii].Description</td>
                    <td>@Model.Items[ii].Amount</td>
                </tr>
            } 
        </tbody>
    </table>
    <button>Test</button>
}
  • 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-06-15T09:15:06+00:00Added an answer on June 15, 2026 at 9:15 am

    Change your post index method as below for the desired behavior:

        [HttpPost]
        public ActionResult Index(TestViewModel model)
        {
           ModelState.Clear();     
    
           model.Items.RemoveAt(0);
    
           return View(model);
        }
    

    Here’s why:

    The TextBoxFor binds to the post value in the ModelState instead of model value. So in your application, when you hit Test button, the text boxes are bound to the value they already have and don’t get updated on postback even after the model values are changed . For ex, If the text boxes in first row are showing 0 and they will remain bound to that value even after post back and the underlying model is having value 1. The reason behind this behavior is validation. If you are expecting an int in the text box and user inputs “SomeTextValue”,the model binder won’t be able to bind to the int property and it will be in a state of validation error. You then would want the user to see an error that says “SomeTextValue” is not an integer. Please enter an integer because you’d expect the user entered value to be there.

    Rick Strahl explains it perfectly in this blog post

    http://www.west-wind.com/weblog/posts/2012/Apr/20/ASPNET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes

    And Brand Wilson below:

    http://forums.asp.net/post/3688022.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with log4net which has been bugging me for a while
I have just noticed something really odd, which I believe might be a small
I don't really know how to describe my issue, so I'll just show a
That's an issue I still don't understand. Sometimes I have to write: NSString* myVariable;
I have an issue which I could not find answer for across the web.
I have some urgent issue which I could not find answer for across the
I have yet another issue which the answer is eluding me. I wish to
i have a small issue which my text field insist to inhert the CSS
I have been working on a json decode issue (which I have already had
I'm pretty new in iphone programming and have stumbled upon this issue which I

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.