我一定是错过了什么,愚蠢的,但问题就在这里。
我在事务控制器上有一个创建操作。 Create.cshtml 使用 jQuery 通过调用 $.ajax 将表单发布到服务器。调试显示一切都按预期到达服务器。我使用表单数据来更新记录:这也很好用。然后,我返回一个部分视图,将模型传递到具有默认数据的视图。我可以调试并验证模型是否传递空值和 0,即模型的默认数据。
问题是,响应中发送回浏览器的是旧数据......!
我看不出有什么理由。希望你能...
注意:我没有使用任何形式的输出缓存。
EDIT 1:
浏览器中没有发生缓存。我这么说的原因是我可以在 Firebug 中看到对 AjaxCreate Action 的调用的响应。我在 Fiddler 中也可以看到这一点。
EDIT 2:
如果您查看部分视图的代码,您将看到每个下拉列表或文本框旁边都打印有 @Model.Transaction.[Property] 的值。奇怪的是,这显示了正确的值,即我的 Transaction 对象的默认值,但下拉列表和文本框坚持使用发布到服务器的值,而不是每个应该呈现的属性的默认值。
EDIT 3:
我已经包含了下图,因此您可以看到打印到传入的每个控件右侧的值。然而,这些控件反映了在先前的 $.ajax 调用中发布到服务器的旧数据。 (注释显示了创建视图模型时的日期时间,这样我就可以看到更新的内容)。
EDIT 4:
我发现用 @Html.TextBox 助手替换 @Html.EditorFor(...) (参见下面的查看代码)可以消除问题。所以,似乎发生的事情是 EditorFor 助手造成了这个问题。为什么?我不知道,但会发布另一个更具体的问题。
![Caching Problem](https://i.stack.imgur.com/09LJZ.jpg)
代码及标记如下:
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),
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>
控制器动作:
[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);
}