2012-08-30 56 views
0

我是新來的ajax和mvc ...我有一個問題,從jquery回發數據。我遇到的問題是正在填充的值控制器是命中數據庫更新,然後它返回到舊數據頁面它不刷新我必須點擊f5才能看到更改我做錯了什麼?由於事先Mvc Ajax Jquery不是刷新視圖上的帖子

Jquery的

    var values = 
         { 
          "PagePartIdentifier": element, 
          "PagePartContent": data 
         } 
         $.post(@Html.Raw(Json.Encode(Url.Action("UploadData", "Home"))),values,function(data) 
         { 
          // do stuff; 
         }); 

模型

public class PagePartModel 
{ 

    public string PagePartIdentifier { get; set; } 
    public string PagePartContent { get; set; } 
} 

控制器

[HttpPost, ValidateInput(false)] 
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")] 
    public ActionResult UploadData(PagePartModel pagePartm) 
    { 
     UpdatePage(pagePartm); 
     ModelState.Clear(); 
     //return RedirectToAction("Index"); 
     return Json(new { success = true }); 

    } 

HTML是由一個輔助方法呈現

public static PagePartModel PageAfterContent(this System.Web.Mvc.HtmlHelper html, int page) 
    { 
     string part = "AfterContent"; 
     Blog.PageParts pageParts = new Blog.PageParts(); 

     PagePartModel thispart = pageParts.GetContentForPageByPart(page, part); 
     return thispart; 
    } 

返回頁面中每個部分的模型

回答

0

使用$ .post時,您通過ajax將數據發送到服務器,因此該頁面不會重新加載。我不確定Post方法中的UpdatePage(pagePartm);調用正在做什麼,但由於該方法使用Ajax調用,因此它不能更改頁面上的任何內容。現在,在$ .post的成功處理程序中,您可以操縱DOM來反映$ .post調用的成功或失敗。

0

在你的行動,而不是返回您Json(new { success = true });你應該/可以返回要在頁面上顯示的數據,

例如

return Json(new { pagePartm }); // if that is what should be returned 

然後修改您的$.post()以將數據追加到DOM的某處。

例如

$.post("your_controller_action", { name: "John", time: "2pm" }, 
    function(data){ 
    doStuff(data); // this is your point of interaction with the returned data 
    // or 
    $("#my_h3").val(data.pagePartm.PagePartIdentifier); 
    $("#my_div").append(data.pagePartm.PagePartContent); 
}); 

from jQuery.post documentation。

這將避免需要刷新頁面以查看結果。

0

jquery ajax post cache property is false?

$.ajax({ 
     url: '/youraction', 
     cache: false, 
     type: 'post' 
     success: function (data) { 


     }, 
     error: function (e) { 
      alert(e.responseText);        
     } 
});