2013-01-22 52 views
0

還有什麼我需要在我的代碼,請,我有這個至今:HttpPost與AJAX調用需要幫助

<script type="text/javascript"> 
function PostNewsComment(newsId) { 
    $.ajax({ 
    url: "<%= Url.Action("AddCommentOnNews", "Home", new { area = "News" }) %>?newsId=" + newsId + "&newsComment=" + $("#textareaforreply").val(), success: function (data) { 
     $("#news-comment-content").html(data + $("#news-comment-content").html()); 
     type: 'POST' 
    } 
    }); 
} 
$("#textareaforreply").val(""); 
</script> 

[HttpPost] 
[NoCache] 
public ActionResult AddCommentOnNews(int newsId, string newsComment) 
{ 
    if (!String.IsNullOrWhiteSpace(newsComment)) 
    { 
    var currentUser = ZincService.GetUserForId(CurrentUser.UserId); 
    ZincService.NewsService.AddCommentOnNews(newsId, newsComment, currentUser.UserId); 

    Zinc.DataModels.News.NewsCommentsDataModel model = new DataModels.News.NewsCommentsDataModel(); 
    var today = DateTime.UtcNow; 
    model.CommentDateAndTime = today; 
    model.NewsComment = newsComment; 
    model.Firstname = currentUser.Firstname; 
    model.Surname = currentUser.Surname; 
    model.UserId = CurrentUser.UserId; 
    return View("NewsComment", model); 
    } 

    return null; 
} 


<div class="actions-right">  
    <a href="javascript:PostNewsComment(<%: Model.News.NewsId %>);" class="button" id="post_button"><%: Html.Resource(Resources.Global.Button.Reply) %></a>     
</div> 

我不知道這是如何工作的,因爲它不在FF中工作? 另一件事是我不能傳遞返回null我必須通過JSON false ???

請幫忙嗎? 謝謝

回答

1

您應該編碼您的請求參數。現在你已經用一個強大的連接把它們連接到請求,這是一個錯誤的方法。有一個叫data屬性,它允許您將參數傳遞到一個AJAX請求,並留下正確的URL編碼框架:

function PostNewsComment(newsId) { 
    $.ajax({ 
     url: '<%= Url.Action("AddCommentOnNews", "Home", new { area = "News" }) %>', 
     type: 'POST', 
     data: { 
      newsId: newsId, 
      newsComment: $('#textareaforreply').val() 
     }, 
     success: function (data) { 
      $('#news-comment-content').html(data + $('#news-comment-content').html()); 
     } 
    }); 
} 

另外你還沒有表現出在何處以及如何被調用這個PostNewsComment功能,但如果這發生在一個鏈接的點擊或提交按鈕確認您已經通過返回false取消默認操作,就像這樣:

$('#someLink').click(function() { 
    PostNewsComment('123'); 
    return false; 
}); 

和另一件事是我不得通過返回NULL我必須通過JSON錯誤?

你可以有你的控制器動作在這種情況下返回JsonResult

return Json(new { success = false }); 

,然後你的成功回調裏面,你可以測試這種情況:

success: function (data) { 
    if (!data.success) { 
     // the server returned a Json result indicating a failure 
     alert('Oops something bad happened on the server'); 
    } else { 
     // the server returned the view => we can go ahead and update our DOM 
     $('#news-comment-content').html(data + $('#news-comment-content').html()); 
    } 
} 

另一件事你應該可能會意識到在評論文本中存在危險字符,如<>。爲了讓這些角色,我會建議你建立一個視圖模型,並與[AllowHtml]屬性裝點相應的屬性:

public class NewsViewModel 
{ 
    public int NewsId { get; set; } 

    [AllowHtml] 
    [Required] 
    public string NewsComment { get; set; } 
} 

現在你的控制器動作,顯然會視圖模型作爲參數:

[HttpPost] 
[NoCache] 
public ActionResult AddCommentOnNews(NewsViewModel viewModel) 
{ 
    if (!ModelState.IsValid) 
    { 
     var currentUser = ZincService.GetUserForId(CurrentUser.UserId); 
     ZincService.NewsService.AddCommentOnNews(viewModel.NewsId, viewModel.NewsComment, currentUser.UserId); 

     var model = new DataModels.News.NewsCommentsDataModel(); 
     var today = DateTime.UtcNow; 
     model.CommentDateAndTime = today; 
     model.NewsComment = newsComment; 
     model.Firstname = currentUser.Firstname; 
     model.Surname = currentUser.Surname; 
     model.UserId = CurrentUser.UserId; 
     return View("NewsComment", model); 
    } 

    return Json(new { success = false }); 
} 
+0

感謝非常很多,但我認爲還是有一些遺漏,我得到了警報('糟糕的事情發生在服務器上')?我的控制器中的回報調用了一個叫做NewsComment的部分,我需要別的東西嗎?謝謝 –

+0

當你調試你的服務器端控制器動作時發生了什麼?操作參數是否正確傳遞? –

+0

着火錯誤: newsComment \t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa NewSID的來源 NewSID的= 120&newsComment = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 動作參數包含正確的價值觀 –