MVC 3 - Html.EditorFor seems to cache old values after $.ajax call

前端 未结 5 1832
一整个雨季
一整个雨季 2020-11-27 14:09

This is a follow on from the following question:

MVC 3 + $.ajax - response seems to be caching output from partial view

There is a detailed description of th

相关标签:
5条回答
  • 2020-11-27 14:46

    This was unexpected behavior for me, and although I understand the reason why it's necessary to give ModelState precedence, I needed a way to remove that entry so that the value from Model is used instead.

    Here are a couple methods I came up with to assist with this. The RemoveStateFor method will take a ModelStateDictionary, a Model, and an expression for the desired property, and remove it.

    HiddenForModel can be used in your View to create a hidden input field using only the value from the Model, by first removing its ModelState entry. (This could easily be expanded for the other helper extension methods).

    /// <summary>
    /// Returns a hidden input field for the specified property. The corresponding value will first be removed from
    /// the ModelState to ensure that the current Model value is shown.
    /// </summary>
    public static MvcHtmlString HiddenForModel<TModel, TProperty>(this HtmlHelper<TModel> helper,
        Expression<Func<TModel, TProperty>> expression)
    {
        RemoveStateFor(helper.ViewData.ModelState, helper.ViewData.Model, expression);
        return helper.HiddenFor(expression);
    }
    
    /// <summary>
    /// Removes the ModelState entry corresponding to the specified property on the model. Call this when changing
    /// Model values on the server after a postback, to prevent ModelState entries from taking precedence.
    /// </summary>
    public static void RemoveStateFor<TModel, TProperty>(this ModelStateDictionary modelState, TModel model,
        Expression<Func<TModel, TProperty>> expression)
    {
        var key = ExpressionHelper.GetExpressionText(expression);
    
        modelState.Remove(key);
    }
    

    Call from a controller like this:

    ModelState.RemoveStateFor(model, m => m.MySubProperty.MySubValue);
    

    or from a view like this:

    @Html.HiddenForModel(m => m.MySubProperty.MySubValue)
    

    It uses System.Web.Mvc.ExpressionHelper to get the name of the ModelState property. This is especially useful when you have "Nested" models since the key name isn't obvious.

    0 讨论(0)
  • 2020-11-27 14:47

    Even if you do not specify caching, it sometimes can occur. For my controllers which handle AJAX and JSON requests, I decorate them as follows:

    [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
    

    This specifically declares no caching should occur.

    UPDATE

    Based on an answer Darin Dimitrov gave here, try adding the following line to your controller action:

    ModelState.Clear();
    
    0 讨论(0)
  • 2020-11-27 14:53

    i have never seen this but basically if you are using ajax to request this data, you need to set nochache: i am assuming you using jQuery.ajax here so will show the code:

    $.ajax({
        url: "somecontroller/someAction,
        cache: false, // this is key to make sure JQUERY does not cache your request
        success: function( data ) {  
             alert( data );
        }
    });
    

    just a stab in the dark, i assume you have probably already covered this already. have you tried to create a new model first and then populate that new instance of the model with your data, and then send this to your view!

    Finally not sure what DB server your using but have you check to see that DB results are not cached and that you are not just requesting SQL results from the DB cache... i dont use MsSQL but i hear that it has outputCaching until something is change on the DB server itself?? anyway just a few thoughts

    0 讨论(0)
  • 2020-11-27 14:55

    There is no caching involved here. It's just how HTML helper work. They first look at the ModelState when binding their values and then in the model. So if you intend to modify any of the POSTed values inside your controller action make sure you remove them from the model state first:

    [HttpPost]
    public virtual ActionResult AjaxCreate(Transaction transaction)
    {
        if (ModelState.IsValid)
        {
            service.InsertOrUpdate(transaction);
            service.Save();
        }
        service.ChosenCostCentreId = transaction.IdCostCentre;
        TransactionViewModel viewModel = new TransactionViewModel();
        ModelState.Remove("Transaction");
        viewModel.Transaction = new Transaction();
        ModelState.Remove("CostCentre");
        viewModel.CostCentre = service.ChosenCostCentre;
        ...
    
        return PartialView("_Create", viewModel);
    }
    
    0 讨论(0)
  • 2020-11-27 14:59

    Make sure you're not doing this:

    @Html.EditorFor(model => model.Transaction.TransactionDate.Date)
    

    I did this, and the model never got the value back. It worked perfectly once I remove the .Date.

    0 讨论(0)
提交回复
热议问题