2012-06-29 50 views
1

编辑使用Ajax使陈旧的结果

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> 
+1

您是否包含了不显眼的ajax库? – VJAI

+0

你能展示你的视图代码吗? –

+0

你在哪里包含'jquery.unobtrusive-ajax.js'脚本?我无法在任何地方看到它。它是否在你的布局? –

回答

0

确定。达林是对的!我发现改变我的控制器代码到这一个:

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

修复了一切。

.Customize((X => x.WaitForNonStaleResults()))

解决!

谢谢大家!