一.新建一个 MVC 2 Empty Web Application.
二.添加一个名叫DataClasses1.dbml的Model,拖放Student表,最后如下图所示.
三.添加一个名叫Home的Controllers.
四.HomeController的Index Action代码如下.
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
Models.DataClasses1DataContext db = new
Models.DataClasses1DataContext();
return
View(db.Students);
}
}
五.增加一个View,选择生成一个强类型的View,选择List.
六.运行程序,会看到如下图所示.
七.下面来改变一下该列表视图.
1.引用MicrosoftAjax.js,MicrosoftMvcAjax.js,添加一个Successed方法.
function Successed() {
alert("数据删除成功!");
}
2.通过Ajax.ActionLink来调用Delete Action来异步删除数据.
{ %>
id="
%>">
"Delete", new { id = item.ID },
new AjaxOptions { Confirm = "确定删除该数据?", OnSuccess
= "Successed" })%>
%>
3.增加一个Delete Action.
public ActionResult Delete(int id)
{
Models.DataClasses1DataContext db = new
Models.DataClasses1DataContext();
Models.Student Student1 = db.Students.Single(s
=> s.ID == id);
db.Students.DeleteOnSubmit(Student1);
db.SubmitChanges();
return Joson("");
}
八.运行程序.