使用Ajax会产生过时的结果

时间:2012-06-29 14:51:08

标签: ajax asp.net-mvc

修改

我的Ajax表单更正Id更新内容和replace选项。 点击<input type="submit" value="submit!" />即可提交。

Problem:当我点击submit时,我没有看到任何更新。第二次试验给出了预期的结果。当我刷新页面时,丢失的记录(首次点击成功)就在现场。

模型

[Serializable]
public abstract class AbstractEntity {
    public Guid Id { get; set; }
    public DateTime LastModified { get; set; }
}

[Serializable]
public class Product : AbstractEntity {
    public Product() {
        this.Attachments = new HashSet<Attachment>();
    }
    public String Title { get; set; }        
    public String Commentary { get; set; }
    public DateTime PlacedOn { get; set; }
    public String User { get; set; }
    public ICollection<Attachment> Attachments { get; set; }
}

[Serializable]
public class Attachment {
    public String MimeType { get; set; }
    public String Description { get; set; }
    public String Filename { get; set; }
}

控制器

[HandleError]
public class ProductController : Controller {
    private readonly IDocumentSession documentSession;

    public ProductController(IDocumentSession documentSession) {
        this.documentSession = documentSession;
    }

    public ActionResult ListRecent() {
        return View(ListAll());
    }

    [Audit, HttpPost]
    public ActionResult Delete(Guid id) {
        documentSession.Delete<Product>(documentSession.Load<Product>(id));
        documentSession.SaveChanges();
        return PartialView("ProductsList", ListAll());
    }

    [Audit, HttpPost]
    public ActionResult Create(Product product) {
        if(ModelState.IsValid) {
            documentSession.Store(product); 
            documentSession.SaveChanges();
        }
        return PartialView("ProductsList", ListAll());
    }

    private IEnumerable<Product> ListAll() {
        return documentSession.Query<Product>().ToArray();
    }
}

观点('无脚本')

  

布局

<head>
    <title>@ViewBag.Title</title>        
    <link href="@Url.Content("~/Content/stylesheets/normalize.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/stylesheets/site.core.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.7.2.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
</head>
<body>
    <div id="main-wrapper">
        <div id="header">[@Html.ActionLink("List", "ListRecent", "Product")]</div>
        <div id="content">@RenderBody()</div>
    </div>        
</body>
  

ListRecent.cshtml

@model IEnumerable<lamp.DomainLayer.Entities.Product>
@{ 
    ViewBag.Title = "ListRecent";
    var options = new AjaxOptions {
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "productsList"        
    };
}

<h2>List</h2>

@Html.Partial("ProductsList", Model)

@using(Ajax.BeginForm("Create", "Product", options)) {
    <fieldset>
        <p>
            <label class="autoWidth">Title</label>
            @Html.Editor("Title")
            @Html.ValidationMessage("Title")
        </p>
        <p>
            <label class="autoWidth">Commentary</label>
            @Html.TextArea("Commentary")
            @Html.ValidationMessage("Commentary")
        </p>
        @* Some fields I have omitted.. *@

        <input type="submit" value="submit" />
        <input type="reset" value="clear" />

    </fieldset>
}
  

ProductsList.cshtml

@model IEnumerable<lamp.DomainLayer.Entities.Product>
@{
    var options = new AjaxOptions {
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "productsList"
    };
}

<div id="productsList">
    @foreach(var p in Model) {
        <div class="productCard">
            <p class="title"><strong>Title</strong>: @p.Title</p>
            <p class="author"><strong>User</strong>: @p.User</p>
            <p class="date"><strong>Placed on</strong>: @idea.PlacedOn.ToShortDateString()</p>
            <p class="link">@Html.ActionLink("details", "Details", "Product")</p>
            <p class="link">
                @using(Ajax.BeginForm("Delete", "Product", new { id = p.Id }, options))  {                
                    <input type="submit" value="you!" />
                }
            </p>
        </div>
    }
</div>

2 个答案:

答案 0 :(得分:2)

我猜这是IE的错(恰恰是IE缓存AJAX请求的方式)。看这里 - 它可能是解决方案:

http://viralpatel.net/blogs/ajax-cache-problem-in-ie/

答案 1 :(得分:0)

确定。达林是对的!我发现将控制器代码更改为此代码:

private IEnumerable<Product> ListAll() {
    return documentSession
       .Query<Product>()
       .Customize((x => x.WaitForNonStaleResults()))
       .ToArray();
}

解决所有问题。

  

.Customize((x =&gt; x.WaitForNonStaleResults()))

是解决方案!

谢谢大家!