MVC 3 - Html.EditorFor 似乎在 $.ajax 调用后缓存旧值 [英] MVC 3 - Html.EditorFor seems to cache old values after $.ajax call

查看:19
本文介绍了MVC 3 - Html.EditorFor 似乎在 $.ajax 调用后缓存旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是以下问题的后续:

MVC 3 + $.ajax - 响应似乎正在缓存部分视图的输出

那边有问题的详细描述.但是,我现在设法缩小了问题的范围,这似乎与 Html.EditorFor 助手有关,因此是新问题.

There is a detailed description of the problem over there. However, I have now managed to narrow down the problem, that seems to be with the Html.EditorFor helpers, hence the new question.

问题:

我使用 $.ajax 将数据发布到服务器,然后返回包含输入控件的部分视图的 html.问题是,尽管将新创建的对象传递给 Partial Views 模型,但各种 @Html.EditorFor 和 @Html.DropDownListFor 助手返回旧数据!

I post data to the server using $.ajax, then return the html of the partial view that holds the input controls. The problem is that, despite passing a newly created object to the Partial Views model, the various @Html.EditorFor and @Html.DropDownListFor helpers return the OLD DATA!.

我可以通过在 Html 助手旁边打印值来证明模型已正确地将新对象传递给助手.即:

I can prove that the model has correctly passed in a new object to the helpers, by printing the value out beside the Html helper. Ie:

@Html.EditorFor(model => model.Transaction.TransactionDate) 
@Model.Transaction.TransactionDate.ToString()

如下图所示,@Html.EditorFor 返回了错误的数据:

As the following image shows, the @Html.EditorFor is returning the wrong data:

[请注意,Commentario 文本框旁边的值是日期时间,因为我正在测试将默认值替换为随每个帖子而变化的值,即 DateTime.]

[Note that the value beside the Comentario text box is a date time, because I was testing replacing the default values with a value that would change with each post, ie, a DateTime.]

如果我用普通的旧 @Html.TextBox() 替换 TransactionDate 的 @Html.EditorFor:

If I replace the @Html.EditorFor for TransactionDate with a plain old @Html.TextBox():

@Html.TextBox("Transaction_TransactionDate", Model.Transaction.TransactionDate)

然后它为新的事务对象呈现正确的 TransactionDate 值,即 DateTime.MinValue (01/01/0001...).

Then it renders the correct TransactionDate value for a new Transaction object, ie, DateTime.MinValue (01/01/0001...).

因此...

问题出在@Html.EditorFor 助手上.TextBoxFor 和 DropDownListFor 也会出现这个问题.

The problem is with the @Html.EditorFor helpers. The problem also happens with TextBoxFor and DropDownListFor.

问题在于这些助手似乎缓存了旧值.

我做错了什么??!

我刚刚尝试在自定义编辑器模板中调试日期,在那里,ViewData.TemplateInfo.FormattedModelValue 显示了正确的值,即01/01/0001".但是,一旦到达 Fiddler,响应就会显示旧日期,例如上图中的01/09/2011".

I have just tried debugging in the custom Editor template for dates, and in there, ViewData.TemplateInfo.FormattedModelValue shows the correct value, ie, "01/01/0001". However, once it gets to Fiddler, the response is showing the old date, eg, "01/09/2011" in the image above.

因此,我只是认为这里有一些缓存,但我没有设置,所以没有任何意义.

As a result, I just think that there is some caching going on here, but I have none set up, so nothing makes any sense.

推荐答案

这里不涉及缓存.这就是 HTML 帮助程序的工作方式.他们在绑定它们的值时首先查看 ModelState,然后在模型中查看.因此,如果您打算修改控制器操作中的任何 POSTed 值,请确保首先将它们从模型状态中删除:

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);
}

这篇关于MVC 3 - Html.EditorFor 似乎在 $.ajax 调用后缓存旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆