MVC删除数据的方法
一种是通过JavaScript:
<scriptsrc="http://www.cnblogs.com/Scripts/MicrosoftAjax.js"type="text/javascript"></script><scripttype="text/javascript">
function deleteRecord(recordId)
{
// Perform delete
var action = "/Home/Delete/" + recordId;
var request = new Sys.Net.WebRequest();
request.set_httpVerb("DELETE");
request.set_url(action);
request.add_completed(deleteCompleted);
request.invoke();
}
function deleteCompleted()
{
// Reload page
window.location.reload();
}
</script>
<td>
@*<%-- Ajax Delete --%> *@
<a onclick="{if(confirm('确认要删除吗?')) deleteRecord(@item.ID)}" href="JavaScript:void(0)">Delete</a>
</td>
附注: javaScript 删除确认实现方法小结
第一种方法:挺好用的,确认以后才能打开下载地址页面。原理也比较清晰。主要用于删除 单条信息确认。 详细出处参考:http://www.jb51.net/article/21543.htm
<SCRIPT LANGUAGE=javascript>
function p_del() {
var msg = "您真的确定要删除吗?\n\n 请确认!"; if (confirm(msg)==true){ return true; }else{ return false; }
}
</SCRIPT>
调用方法:
<a href="del.jsp?id=<%=id%>" onclick="javascript:return p_del()">删 除</a>
第二种方法:原理跟上面的一样。 JavaScript 删除确认框
<a href="javascript:if(confirm('确实要删除吗?'))location='jb51.php?id='">删除</a>
第三种:主要用于批量删除的确认提示
<input name="Submit" type="submit" class="inputedit" value="删除" onclick="{if(confirm('确 定纪录吗?')){this.document.formname.submit();return true;}return false;}">
<input name=" 按 钮 " type="button" ID="ok" onclick="{if(confirm(' 确 定 删 除 吗 ?')){window.location='Action.asp?Action=Del&TableName=Item&ID=<%=ID%>';return true;}return false;}" value="删除栏目" />
另一种是通过Http Post:
Controller里面的Delete这样写:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int id)
{
var movieToDelete = (from m in _entities.MovieSet
where m.Id == id
select m).FirstOrDefault();
_entities.DeleteObject(movieToDelete);
_entities.SaveChanges();
return RedirectToAction("Index");
}
View里面这样用:
<td>
<% using (Html.BeginForm("Delete", "Home", new { id = item.Id }))
{ %>
<input type="image" src="Content/Delete.png" />
<% } %>
</td>