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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:18:41+00:00 2026-05-27T19:18:41+00:00

This question is a bit different than most. My code works but I don’t

  • 0

This question is a bit different than most. My code works but I don’t understand why it works.

I am trying to understand why changes made in the form get persisted after posting to the server.

Model:

public class TestUpdateModel
{
    public int Id { get; set; }
    public List<CarrierPrice> Prices { get; set; }

    public TestUpdateModel() { } // parameterless constructor for the modelbinder

    public TestUpdateModel(int id)
    {
        Id = id;
        Prices = new List<CarrierPrice>();
        using (ProjectDb db = new ProjectDb())
        {
            var carriers = (from c in db.Carriers
                            select c);

            foreach (var item in carriers)
            {
                var thesePrices = item.Prices.Where(x => x.Parent.ParentId == Id);
                if (thesePrices.Count() <= 0)
                {
                    Prices.Add(new CarrierPrice
                    {
                        Carrier = item
                    });
                }
                else
                    Prices.Add(thesePrices.OrderByDescending(x => x.DateCreated).First());
            }
        }
    }
}

Controller:

public ViewResult Test(int id = 1)
{
    TestUpdateModel model = new TestUpdateModel(id);
    return View(model);
}

[HttpPost]
public ViewResult Test(TestUpdateModel model)
{
    model = new TestUpdateModel(model.Id);
    return View(model); // when model is sent back to view, it keeps the posted changes... why?
}

View

@model Namespace.TestUpdateModel

@{ ViewBag.Title = "Test"; }

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

        @Html.HiddenFor(model => model.Id)
        @Html.EditorFor(x => x.Prices)
        <input type="submit" value="Save" />
}

EditorTemplate

@model Namespace.CarrierPrice
    <tr>
        <th>
            @Html.HiddenFor(model => model.CarrierPriceId)
            @Html.HiddenFor(model => model.DateCreated)
            @Model.Carrier.Name
        </th>
        <td>
            $@Html.TextBoxFor(model => model.Fee)
        </td>
    </tr>

The Source of My Confusion

1) Load the page in the browser

2) Change the value of the model.fee TextBox

3) Submit

At this point I would expect that model = new TestUpdateModel(model.Id); would create a new TestUpdateModel object and wipe out my changes so the original values re-appear when the view is returned. But what actually happens is my changes in the form are persisted to the postback.

Why does this happen?

Thanks for any help.

  • 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-27T19:18:42+00:00Added an answer on May 27, 2026 at 7:18 pm

    No. The reason is that the View Engine looks in the ModelState to fill your values before it looks at the model when it renders your view. If there are posted values, then it will find those first and use them.

    If you want to override this behavior, then you need to clear the ModelState first with:

    ModelState.Clear();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed This question , but my question is a bit more specific. Is
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
I know this question has been asked a bit before. But looking around I
Most of you will realize this, but to me it came a bit as
This question mixes a bit of project management as well as development. I understand
This is a homework question, but as you can see I've written most of
This question is a bit of a two parter for .Net data services. This
This question is a bit open ended as I'm not sure it's possible or
This question is a bit of a two-parter. First, the title question. Here's what
Apologies if this question is a bit obscure, I've been banging my head against

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.