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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:33:19+00:00 2026-05-25T17:33:19+00:00

I must be missing something, silly, but here is the problem. I have a

  • 0

I must be missing something, silly, but here is the problem.

I have a Create action on the Transactions controller. The Create.cshtml uses jQuery to post the form to the server using a call to $.ajax. Debugging shows that everything arrives on the server as expected. I use the form data to update a record: this works fine too. I then return a partial view, passing a model to the view with default data. I can debug and verify that the model is passing nulls and 0s, ie, the default data for my model.

Problem is, what gets sent back to the browser in the response is the old data…!

I can see no reason why. Hopefully, you can…

Note: I am not using any form of output cache.

EDIT 1:

The caching is not happening in the browser. The reason I say that is that I can see in Firebug the response of the call to the AjaxCreate Action. I can also see this in Fiddler.

EDIT 2:

If you look at the code for the Partial View, you will see that each dropdownlist or textbox has the value of @Model.Transaction.[Property] printed out beside it. This, bizarrely, shows the correct value, ie, the defaults for my Transaction object, but the dropdownlists and text boxes stick with the values that were posted to the server rather than the default values for the property each one is supposed to render.

EDIT 3:

I have included the following image, so you can see the values printed to the right of each control that are being passed in. And yet the controls reflect the old data posted to the server in the previous $.ajax call. (The comment shows a date time at the moment of creating the view model, that way I could see things updating).

EDIT 4:

I have found that replacing @Html.EditorFor(…) (see view code below) with @Html.TextBox helpers removes the problem. So, what seems to be happening is that the EditorFor helpers are causing the problem. Why? I have no idea, but will post another, more specific question.

Caching Problem

Code and markup as follows:

jQuery:

$(document).ready(function () {

    $('input[name="nextRecord"]').live('click', function () {
        var theForm = $(this).closest('form');
        if ((theForm).valid()) {
            var buttonText = $(this).val();
            var action = "/input/Transactions/AjaxCreate/";
            if (buttonText === "Reset") {
                clearForm(theForm);
            }
            else {
                var targetElement = $('#CreateDiv');
                var _data = theForm.serialize() + '&nextRecord=' + $(this).val();

                $.ajax({
                    url: action,
                    data: _data,
                    cache: 'false',
                    type: 'POST',
                    dataType: 'html',
                    success: function (html) {
                        $(targetElement).html(html);
                        createDatePickers(targetElement);
                        jQuery.validator.unobtrusive.parse(targetElement);
                    }
                });
            }
        }
        return false;
    });
});

Partial View:

@model FlatAdmin.Domain.ViewModels.TransactionViewModel

@* This partial view defines form fields that will appear when creating and editing entities *@

<div class="editor-label">
    Fecha
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Transaction.TransactionDate, new { @class = "date-picker" })
    @Html.ValidationMessageFor(model => model.Transaction.TransactionDate) @Model.Transaction.TransactionDate.ToString()
</div>

<div class="editor-label">
    Origen:
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.Transaction.IdFrom, ((IEnumerable<FlatAdmin.Domain.Entities.Account>)Model.FromAccounts).Select(option => new SelectListItem
{
        Text = (option == null ? "None" : option.AccountName), 
        Value = option.AccountId.ToString(),
        Selected = (Model != null) && (option.AccountId == Model.Transaction.IdFrom)
    }), "Choose...")
    @Html.ValidationMessageFor(model => model.Transaction.IdFrom)@Model.Transaction.IdFrom
</div>

<div class="editor-label">
    Destino:
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.Transaction.IdTo, ((IEnumerable<FlatAdmin.Domain.Entities.Account>)Model.ToAccounts).Select(option => new SelectListItem
{
    Text = (option == null ? "None" : option.AccountName),
    Value = option.AccountId.ToString(),
    Selected = (Model != null) && (option.AccountId == Model.Transaction.IdTo)
}), "Choose...")
    @Html.ValidationMessageFor(model => model.Transaction.IdTo)@Model.Transaction.IdTo
</div>
<div class="editor-label">
    Monto
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.Transaction.IdCurrency, ((IEnumerable<FlatAdmin.Domain.Entities.Currency>)Model.AllCurrencies).Select(option => new SelectListItem
{
    Text = (option == null ? "None" : option.CurrencyName),
    Value = option.CurrencyId.ToString(),
    Selected = (Model != null) && (option.CurrencyId == Model.Transaction.IdCurrency)
})) 
    @Html.EditorFor(model => model.Transaction.Amount)
    @Html.ValidationMessageFor(model => model.Transaction.Amount) @Model.Transaction.Amount
</div>

<div class="editor-label">
    Comentario
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Transaction.Comment)
    @Html.ValidationMessageFor(model => model.Transaction.Comment)@Model.Transaction.Comment
</div>

View:

@model FlatAdmin.Domain.ViewModels.TransactionViewModel
@using FlatAdmin.Domain.Entities

@{
    ViewBag.Title = "Nueva Transaccion";
}

<h2>@ViewBag.Title</h2>
<div>
    @Html.ActionLink("<< Lista de Transacciones", "Index")
</div>
<br />

<div id="InputPanel">
    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Elegir Actividad</legend>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Transaction.IdCostCentre, ((IEnumerable<FlatAdmin.Domain.Entities.CostCentre>)Model.AllCostCentres).Select(option => new SelectListItem
           {
               Text = (option == null ? "None" : option.Name),
               Value = option.CostCentreId.ToString(),
               Selected = (Model != null) && (option.CostCentreId == Model.Transaction.IdFrom)
           }), "Actividades...")
            </div>
        </fieldset>
        <fieldset>
            <legend>Transaccion</legend>
            <div id="CreateDiv">
                @Html.Partial("_Create", Model)
            </div>
            <p>
                <input type="submit" name="nextRecord" value="Proxima Transaccion >>" />
            </p>
            <p>
                ...o sino, para guardar y volver a la lista de transacciones:<br /><input type="submit" value="Guardar" />
            </p>

        </fieldset>
    }
</div>

Controller Action:

[HttpPost]
public virtual ActionResult AjaxCreate(Transaction transaction)
{
    if (ModelState.IsValid)
    {
        service.InsertOrUpdate(transaction);
        service.Save();
    }
    service.ChosenCostCentreId = transaction.IdCostCentre;
    TransactionViewModel viewModel = new TransactionViewModel();
    viewModel.Transaction =  new Transaction();
    viewModel.CostCentre = service.ChosenCostCentre;
    viewModel.AllCostCentres = service.AllCostCentres;
    viewModel.AllCurrencies = service.AllCurrencies;
    viewModel.FromAccounts = service.FromAccounts;
    viewModel.ToAccounts = service.ToAccounts;

    return PartialView("_Create", viewModel);
}
  • 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-25T17:33:19+00:00Added an answer on May 25, 2026 at 5:33 pm

    @Darin Dimitrov came up with the answer in a related thread.

    Essentially, the HtmlHelpers such as Html.EditorFor, Html.TextBoxFor, etc, check first in the ModelState for existing values, and ONLY then in the Model.

    As a result, I needed a call to:

    ModelState.Clear();
    

    Ignorance is so painful.

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

Sidebar

Related Questions

I must be missing something silly here. I have this: case class Color(val rgb:Int)
I know SQL well but I must be missing something really dumb here. This
This seems very basic and I must be missing something, but here goes anyways...
I must be missing something simple here, but I'm having trouble retrieving data from
I must be missing something stupid here but I can not see it. My
I know I must be missing something here, but I cannot seem to get
I must be missing something here, but I seem to be having some trouble
I must be missing something here, but I've got an instance of my submission
I must be missing something obvious here, but I can't seem to get the
Must be missing something here, but I'm using Node_redis as Node.js client for Redis.

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.