最近去了解了一下 asp.net mvc ,就讲一下“删除”吧。
在显示数据列表的页面做删除功能,首先http在web中支持get和post请求两种,
如果想在同一页面显示数据并且做删除操作可以通过ajax调用来支持更多的请求方式(put,delete,head等)。
首先是显示数据列表:
Controller代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public ActionResult Index(int? pageIndex)
{
PageList<Movie> list = new PageList<Movie>();
IQueryable<Movie> query = from m in movie.Movie select m;
int pageSize = 10;
int currentPage = pageIndex ?? 1;
int count = 0;
count = query.Count();
list.RowCount = count;
list.PageSize = pageSize;
currentPage = currentPage < 1 ? 1 : currentPage > list.TotalPages ? list.TotalPages : currentPage;
list.CurrentPage = currentPage;
if (count < 1)
{
list.List = new List<Movie>();
}
else
{
list.List = query.Skip<Movie>((currentPage - 1) * pageSize).Take(pageSize);
}
return View(list);
}说明:PageList<>类是自定义的一个分页类。
显示数据:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<table id="data">
<tr>
<th>
Id
</th>
<th>
MovieName
</th>
<th>
CategoryName
</th>
<th>Details</th>
<th>Update</th>
<th>Delete</th>
</tr>
<% foreach (var item in (Model as Common.PageList.PageList<MvcDemo.Models.Movie>).List) { %>
<tr>
<td>
<%= Html.Encode(item.Id) %>
</td>
<td>
<%= Html.Encode(item.MovieName) %>
</td>
<td>
<%= Html.Encode(item.CategoryName) %>
</td>
<td>
<%= Html.ActionLink("详情","Details",new{id = item.Id}) %>
</td>
<td>
<%= Html.ActionLink("更新", "Edit", new {id=item.Id})%>
</td>
<td><a href="javascript:void(0);" onclick="del(this,<%= item.Id %>);">删除</a></td>
</tr>
<% } %>
</table>从上可以看到删除用到了del方法。代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
function del(obj,mId)
{
if (confirm("确定删除?"))
{
$.ajax(
{
url: "/Movie/Delete/" + mId,
type: "DELETE",
success: function()
{
$(obj.parentNode.parentNode.parentNode.rows[obj.parentNode.parentNode.rowIndex]).remove();
alert("删除成功!");
//window.location.reload();
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert("删除失败!\n信息:" + errorThrown + "," + textStatus + "\n");
}
}
);
}
}
在此引用了jquery的ajax功能,在删除的时候请求的方法是DELETE,由上面的type:"DELETE"指定。
服务器端的删除代码就如下,都比较简单:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
[AcceptVerbs(HttpVerbs.Delete)]
public void Delete(int id)
{
try
{
Movie mov = (from m in movie.Movie where m.Id == id select m).First();
movie.Movie.DeleteOnSubmit(mov);
movie.SubmitChanges();
}
catch
{ }
}
在显示数据列表的页面做删除功能,首先http在web中支持get和post请求两种,
如果想在同一页面显示数据并且做删除操作可以通过ajax调用来支持更多的请求方式(put,delete,head等)。
首先是显示数据列表:
Controller代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public ActionResult Index(int? pageIndex)
{
PageList<Movie> list = new PageList<Movie>();
IQueryable<Movie> query = from m in movie.Movie select m;
int pageSize = 10;
int currentPage = pageIndex ?? 1;
int count = 0;
count = query.Count();
list.RowCount = count;
list.PageSize = pageSize;
currentPage = currentPage < 1 ? 1 : currentPage > list.TotalPages ? list.TotalPages : currentPage;
list.CurrentPage = currentPage;
if (count < 1)
{
list.List = new List<Movie>();
}
else
{
list.List = query.Skip<Movie>((currentPage - 1) * pageSize).Take(pageSize);
}
return View(list);
}
显示数据:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<table id="data">
<tr>
<th>
Id
</th>
<th>
MovieName
</th>
<th>
CategoryName
</th>
<th>Details</th>
<th>Update</th>
<th>Delete</th>
</tr>
<% foreach (var item in (Model as Common.PageList.PageList<MvcDemo.Models.Movie>).List) { %>
<tr>
<td>
<%= Html.Encode(item.Id) %>
</td>
<td>
<%= Html.Encode(item.MovieName) %>
</td>
<td>
<%= Html.Encode(item.CategoryName) %>
</td>
<td>
<%= Html.ActionLink("详情","Details",new{id = item.Id}) %>
</td>
<td>
<%= Html.ActionLink("更新", "Edit", new {id=item.Id})%>
</td>
<td><a href="javascript:void(0);" onclick="del(this,<%= item.Id %>);">删除</a></td>
</tr>
<% } %>
</table>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function del(obj,mId)
{
if (confirm("确定删除?"))
{
$.ajax(
{
url: "/Movie/Delete/" + mId,
type: "DELETE",
success: function()
{
$(obj.parentNode.parentNode.parentNode.rows[obj.parentNode.parentNode.rowIndex]).remove();
alert("删除成功!");
//window.location.reload();
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert("删除失败!\n信息:" + errorThrown + "," + textStatus + "\n");
}
}
);
}
}
服务器端的删除代码就如下,都比较简单:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
[AcceptVerbs(HttpVerbs.Delete)]
public void Delete(int id)
{
try
{
Movie mov = (from m in movie.Movie where m.Id == id select m).First();
movie.Movie.DeleteOnSubmit(mov);
movie.SubmitChanges();
}
catch
{ }
}