2011-04-30 57 views
0

我正在看一個教程,顯示如何調用MVC操作並傳遞參數。 我有一個動態的「評論框」,我需要保存到數據庫。 我想使用jQuery將註釋數據發送到將處理它的REST方法。尋找與jQuery MVC 3教程

我還需要用MVC操作返回的數據刷新頁面的一部分。 以JSON形式返回的數據。

我已經看過Scott Guthrie的tutoral,但是使用了回傳。 我需要通過jQuery進行異步通信。

非常簡單和小的教程將是非常有用的。

感謝

編輯: 我將使用jQuery

回答

2

$.ajax()電話假設你有你的觀點的形式,將允許用戶發表評論:

@using (Html.BeginForm("Save", "Comment", FormMethod.Post, new { id = "commentForm" })) 
{ 
    @Html.TextAreaFor(x => x.Comment) 
    <input type="submit" value="Comment" /> 
} 
<div id="result"></div> 

你可以使用jQuery對其進行AJAX化:

$(function() { 
    $('#commentForm').submit(function() { 
     $.ajax({ 
      url: this.action, 
      type: this.method, 
      data: $(this).serialize(), 
      success: function(result) { 
       // refresh some part of the DOM based ion the result 
       $('#result').html(result.someProperty); 
      } 
     }); 
     return false; 
    }); 
}); 

和控制器動作,這將保存註釋,並返回可能在成功回調中使用的JSON對象:

[HttpPost] 
public ActionResult Save(string comment) 
{ 
    // TODO: save the comment 
    return Json(new { someProperty = "some value" }); 
} 

而且here's a tutorial約漸進增強與ASP.NET MVC 3和jQuery。

+0

沒有單獨的表格。我動態地添加一個帶有「發佈」和「取消」按鈕的文本框。點擊「發佈」按鈕將調用js函數發佈。你的迴應有我需要的東西。讓我試試看。 – kheya 2011-04-30 06:18:41