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

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

问题描述

这是从以下问题上遵循:

This is a follow on from the following question:

<一个href=\"http://stackoverflow.com/questions/7404334/mvc-3-ajax-response-seems-to-be-caching-output-from-partial-view\">MVC 3 + $阿贾克斯 - 反应似乎从缓存局部视图输出

有是问题的那边的详细描述。不过,我现在已经成功地缩小问题,这似乎是与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.

问题:

我发布的数据使用$阿贾克斯服务器,然后返回包含输入控制局部视图的HTML。的问题是,尽管通过一个新创建的对象到局部视图模型,各种@ 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:

[注意Comentario文本框旁边的值是一个日期时间,因为我在测试与将与每一个岗位,即一个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.EditorFor为TransactionDate与普通的旧@ Html.TextBox():

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

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

然后,它呈现了一个新的交易对象,也就是说,DateTime.MinValue(01/01/0001 ...)正确TransactionDate值。

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。但是,一旦它得到提琴手,响应显出老日期,例如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模型结合自己的价值观,然后当。

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似乎后阿贾克斯$调用缓存旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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