asp.net-mvc-3 – MVC 3 $.ajax – 响应似乎是从部分视图缓存输出

我一定是想念一些傻瓜,但这就是问题所在.

我在Transactions控制器上有一个Create动作. Create.cshtml使用jQuery通过调用$.ajax将表单发布到服务器.调试显示所有内容都按预期到达服务器.我使用表单数据来更新记录:这也可以正常工作.然后我返回一个局部视图,将模型传递给具有默认数据的视图.我可以调试并验证模型是否传递空值和0,即我的模型的默认数据.

问题是,在响应中发送回浏览器的是旧数据……!

我没有理由看到原因.希望你能……

注意:我没有使用任何形式的输出缓存.

编辑1:

缓存不会在浏览器中发生.我说的原因是我可以在Firebug中看到对AjaxCreate Action的调用的响应.我也可以在Fiddler中看到这一点.

编辑2:

如果查看部分视图的代码,您会看到每个下拉列表或文本框的值都是@ Model.Transaction.[Property]打印在它旁边.奇怪的是,这显示了正确的值,即我的Transaction对象的默认值,但是下拉列表和文本框坚持使用发布到服务器的值,而不是每个应该呈现的属性的默认值.

编辑3:

我已经包含了下面的图像,因此您可以看到正在传入的每个控件右侧打印的值.然而,控件反映了在之前的$.ajax调用中发布到服务器的旧数据. (注释显示创建视图模型时的日期时间,这样我可以看到更新的内容).

编辑4:

我发现用@ Html.TextBox助手替换@ Html.EditorFor(…)(参见下面的视图代码)可以解决问题.所以,似乎正在发生的事情是EditorFor助手正在引发问题.为什么?我不知道,但会发布另一个更具体的问题.

代码标记如下:

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

局部视图:

@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),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>

视图:

@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>

控制器动作:

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

解决方法

@Darin Dimitrov在相关主题中提出了答案.

本质上,HtmlHelpers(如Html.EditorFor,Html.TextBoxFor等)首先在ModelState中检查现有值,然后仅在模型中检查.

结果,我需要打电话给:

ModelState.Clear();

无知是如此痛苦.

相关文章

项目要求通过网站上传大文件,比如视频文件,通过摸索实现了文件分片来上传,然后后台进行合并。 使用了...
安装新版本的Nginx(vim /etc/yum.repos.d/nginx.repo) [nginx-stable] name=nginx stable repo baseu...
什么是 SignalR&#160;ASP.NET Core ASP.NET Core SignalR 是一种开放源代码库,可简化将实时 web 功...
在Windows下使用Docker,我们选择Docker Desktop这个软件,非常方便。 ## Docker Desktop介绍及安装 Do...
项目开始设计的是运行在windows下,所以一开始采用的是windows服务模式来获取多媒体文件信息,后来要求...
银河麒麟高级服务器操作系统V10是针对企业级关键业务,适应虚拟化、云计算、大数据、工业互联网时代对主...