weixin_33709590 2012-06-29 14:51 采纳率: 0%
浏览 50

使用Ajax会产生过时的结果

EDIT

My Ajax form gets correct Id to update a content and a replace option. Submitting is by clicking <input type="submit" value="submit!" />.

Problem: When I clicked on submit I didn't see any update. Second trial gave the expected result. When I refreshed the page the lost record (first hit was successful) was on spot.

Model

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

Controller

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

Views ('scriptless')

Layout

<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条回答 默认 最新

  • 乱世@小熊 2012-07-02 16:42
    关注

    I guess it is IE's fault (precisely the way,how IE cache AJAX requests). Look here-it could be solution:

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

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP