2012-08-30 51 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);        
     } 
});