wgp*_*ubs 6 asp.net-mvc asp.net-mvc-3
是否有可能强迫@Html.ActionLink()做一个POST而不是一个GET?如果是这样,怎么样?
ActionLinkhelper方法将呈现一个anchor标记,点击它始终是一个GET请求.如果你想提出POST要求.你应该使用一点javacsript覆盖默认的behviour
@ActionLink("Delete","Delete","Item",new {@id=4},new { @class="postLink"})
Run Code Online (Sandbox Code Playgroud)
现在一些jQuery代码
<script type="text/javascript">
$(function(){
$("a.postLink").click(function(e){
e.preventDefault();
$.post($(this).attr("href"),function(data){
// got the result in data variable. do whatever you want now
//may be reload the page
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
确保您有一个类型的Action方法HttpPost来处理此请求
[HttpPost]
public ActionResult Delete(int id)
{
// do something awesome here and return something
}
Run Code Online (Sandbox Code Playgroud)
我想如果你需要类似的东西,那就是在服务器端做一些"永久"的动作.例如,删除数据库中的对象.
以下是使用链接和发布删除的完整示例:http: //www.squarewidget.com/Delete-Like-a-Rock-Star-with-MVC3-Ajax-and-jQuery
从上一个链接(无论如何推荐阅读):
您视图中的删除链接:
@Ajax.ActionLink("Delete", "Delete", "Widget",
new {id = item.Id},
new AjaxOptions {
HttpMethod = "POST",
Confirm = "Are you sure you want to delete this widget?",
OnSuccess = "deleteConfirmation"
})
Run Code Online (Sandbox Code Playgroud)
一点JS:
function deleteConfirmation(response, status, data) {
// remove the row from the table
var rowId = "#widget-id-" + response.id;
$('.widgets').find(rowId).remove();
// display a status message with highlight
$('#actionMessage').text(response.message);
$('#actionMessage').effect("highlight", {}, 3000);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16999 次 |
| 最近记录: |