我一定是在遗漏一些东西,愚蠢,但这就是问题所在。
我在Transactions控制器上有一个Create动作。 Create.cshtml使用jQuery通过调用$ .ajax将表单发布到服务器。调试显示所有内容都按预期到达服务器。我使用表单数据来更新记录:这也可以正常工作。然后我返回一个局部视图,将模型传递给具有默认数据的视图。我可以调试并验证模型是否传递空值和0,即我的模型的默认数据。
问题是,在响应中发送回浏览器的是旧数据......!
我看不出原因。希望你能...
注意:我没有使用任何形式的输出缓存。
编辑1:
浏览器中没有发生缓存。我说的原因是我可以在Firebug中看到对AjaxCreate Action的调用的响应。我也可以在Fiddler中看到这一点。
编辑2:
如果您查看部分视图的代码,您会看到每个下拉列表或文本框的值都是@ Model.Transaction。[Property]打印在它旁边。奇怪的是,这显示了正确的值,即我的Transaction对象的默认值,但是下拉列表和文本框坚持使用发布到服务器的值而不是每个应该呈现的属性的默认值。 / p>
我已经包含了以下图像,因此您可以看到每个控件右侧打印的值。但是控件反映了之前$ .ajax调用中发布到服务器的旧数据。 (注释显示创建视图模型时的日期时间,这样我可以看到更新的内容)。
我发现用@ Html.TextBox助手替换@ Html.EditorFor(...)(参见下面的视图代码)可以解决问题。所以,似乎正在发生的事情是EditorFor助手正在引发问题。为什么?我不知道,但会发布另一个更具体的问题。
代码和标记如下:
$(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>
@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);
}
答案 0 :(得分:13)
基本上,HtmlHelpers(如Html.EditorFor,Html.TextBoxFor等)首先在ModelState中检查现有值,然后仅检查模型。
结果,我需要打电话给:
ModelState.Clear();
无知是如此痛苦。
答案 1 :(得分:0)
尝试在控制器操作上明确将outputcache duration设置为0。
我认为浏览器不应该缓存POST,但有时似乎仍然会这样做。