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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:59:50+00:00 2026-05-10T18:59:50+00:00

When I use the default model binding to bind form parameters to a complex

  • 0

When I use the default model binding to bind form parameters to a complex object which is a parameter to an action, the framework remembers the values passed to the first request, meaning that any subsequent request to that action gets the same data as the first. The parameter values and validation state are persisted between unrelated web requests.

Here is my controller code (service represents access to the back end of the app):

    [AcceptVerbs(HttpVerbs.Get)]     public ActionResult Create()     {         return View(RunTime.Default);     }      [AcceptVerbs(HttpVerbs.Post)]     public ActionResult Create(RunTime newRunTime)     {         if (ModelState.IsValid)         {             service.CreateNewRun(newRunTime);             TempData['Message'] = 'New run created';             return RedirectToAction('index');         }         return View(newRunTime);     } 

My .aspx view (strongly typed as ViewPage<RunTime>) contains directives like:

<%= Html.TextBox('newRunTime.Time', ViewData.Model.Time) %> 

This uses the DefaultModelBinder class, which is meant to autobind my model’s properties.

I hit the page, enter valid data (e.g. time = 1). The app correctly saves the new object with time = 1. I then hit it again, enter different valid data (e.g. time = 2). However the data that gets saved is the original (e.g. time = 1). This also affects validation, so if my original data was invalid, then all data I enter in the future is considered invalid. Restarting IIS or rebuilding my code flushes the persisted state.

I can fix the problem by writing my own hard-coded model binder, a basic naive example of which is shown below.

    [AcceptVerbs(HttpVerbs.Post)]     public ActionResult Create([ModelBinder(typeof (RunTimeBinder))] RunTime newRunTime)     {         if (ModelState.IsValid)         {             service.CreateNewRun(newRunTime);             TempData['Message'] = 'New run created';             return RedirectToAction('index');         }         return View(newRunTime);     }   internal class RunTimeBinder : DefaultModelBinder {     public override ModelBinderResult BindModel(ModelBindingContext bindingContext)     {         // Without this line, failed validation state persists between requests         bindingContext.ModelState.Clear();           double time = 0;         try         {             time = Convert.ToDouble(bindingContext.HttpContext.Request[bindingContext.ModelName + '.Time']);         }         catch (FormatException)         {             bindingContext.ModelState.AddModelError(bindingContext.ModelName + '.Time', bindingContext.HttpContext.Request[bindingContext.ModelName + '.Time'] + 'is not a valid number');         }          var model = new RunTime(time);         return new ModelBinderResult(model);     } } 

Am I missing something? I don’t think it’s a browser session problem as I can reproduce the problem if the first data is entered in one browser and the second in another.

  • 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. 2026-05-10T18:59:50+00:00Added an answer on May 10, 2026 at 6:59 pm

    It turns out that the problem was that my controllers were being reused between calls. One of the details I chose to omit from my original post is that I am using the Castle.Windsor container to create my controllers. I had failed to mark my controller with the Transient lifestyle, so I was getting the same instance back on each request. Thus the context being used by the binder was being re-used and of course it contained stale data.

    I discovered the problem while carefully analysing the difference between Eilon’s code and mine, eliminating all other possibilities. As the Castle documentation says, this is a ‘terrible mistake’! Let this be a warning to others!

    Thanks for your response Eilon – sorry to take up your time.

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

Sidebar

Ask A Question

Stats

  • Questions 213k
  • Answers 213k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would highly recommend using the in-app purchasing over having… May 12, 2026 at 10:36 pm
  • Editorial Team
    Editorial Team added an answer I definitely wouldn't choose the option of not using a… May 12, 2026 at 10:36 pm
  • Editorial Team
    Editorial Team added an answer Unfortunately, your options are to change either your class or… May 12, 2026 at 10:36 pm

Related Questions

I am using the Data Annotation Validator, outlined here: http://www.asp.net/learn/mvc/tutorial-39-cs.aspx The Data Annotations Model
I'm just starting to play with asp.net mvc and I've got a very basic
I have a section of my site where users can add addresses to their
The behavior described here appears to now be the default for ASP.NET MVC 2

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.