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