Linq to sql 让MVC2删除生效
好的,我有一个mvc应用程序。我正在努力让我的删除生效。基本上我想要它,所以当我点击删除它会带我到一个页面说“你确定吗?”我有工作,问题是捕捉请求并实际执行删除。我尝试了不同的方法。如下Linq to sql 让MVC2删除生效,linq-to-sql,Linq To Sql,好的,我有一个mvc应用程序。我正在努力让我的删除生效。基本上我想要它,所以当我点击删除它会带我到一个页面说“你确定吗?”我有工作,问题是捕捉请求并实际执行删除。我尝试了不同的方法。如下 public ActionResult Delete(int id) { var something = friendsDB.Friends.Single(a => a.Id == id); return View(something); } [H
public ActionResult Delete(int id)
{
var something = friendsDB.Friends.Single(a => a.Id == id);
return View(something);
}
[HttpPost]
public ActionResult Delete(int id, string confirmButton)
{
var something = friendsDB.Friends.Single(a => a.Id == id);
// For simplicity, we're allowing deleting of albums
// with existing orders We've set up OnDelete = Cascade
// on the Album->OrderDetails and Album->Carts relationships
friendsDB.DeleteObject(something);
friendsDB.SaveChanges();
return View("Index");
}
这不起作用,因为deleteobject和savechanges声明
“C:\Users\Mtszc\Documents\Visual
演播室
2010\Projects\Test\Test\Content\Controllers\DownloadsController.cs(36,23):
错误CS1061:“Test.Models.FriendsDB”
不包含的定义
“DeleteObject”和无扩展方法
“DeleteObject”接受第一个
类型参数
可以找到“Test.Models.FriendsDB”
(您是否缺少using指令或
程序集引用?“
我试过的第二件事是
public ActionResult Delete(int id)
{
var something = friendsDB.Friends.Single(a => a.Id == id);
return View(something);
}
[HttpDelete]
public ActionResult Delete(Friend myFriend)
{
try
{
friendsDB.Friends.DeleteOnSubmit(myFriend);
friendsDB.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
那没用。它已编译,但当我单击“删除”时,它会将我带到我说“我确定要删除”的页面,它会返回视图,这是捕获,意味着尝试失败
这是我制作的一个简单的sql数据库,Id、名称、链接。我使用LINQtoSQL类。IC可以创建和查看,但不能删除。错误消息似乎让您感到困惑
"C:\Users\Mtszc\Documents\Visual Studio 2010
\Projects\Test\Test\Content\Controllers\DownloadsController.cs(36,23): error
CS1061: 'Test.Models.FriendsDB' does not contain a definition for 'DeleteObject'
and no extension method 'DeleteObject' accepting a first argument of
type 'Test.Models.FriendsDB' could be found (are you missing a using directive
or an assembly reference?)"
不是指MVC操作,而是指您的Test.Models.FriendsDB方法调用:
friendsDB.DeleteObject(something);
听起来您没有在friendsDB模型上定义方法“DeleteObject”,或者您没有接受“Test.Models.friendsDB”对象类型的重载方法
其次,不要将HTTP方法(Get、Post、Put、Delete)与您试图实现的内容混淆。“放置”和“删除”是我不相信web浏览器经常使用的方法。大多数请求都是GET,除非您提交表单,否则它们都是POST。添加HttpDelete很可能会使该操作无效。如果您只想从表单提交中删除操作,请添加HttpPost,尝试类似的操作
var rowToDelete = friendsDB.Friends.Single(a => a.ID == myFriend.ID);
friendsDB.Friends.DeleteOnSubmit(rowToDelete);
friendsDB.SubmitChanges();
这将是使用Linq处理记录删除的一种简单方法。如果语法不完美,我表示歉意,因为我是在脑子里胡乱写的
顺便说一句,微软的同事们制作了一些很棒的视频,用于学习ASP.NETMVC和LINQ。看看这些
干杯好的,谁看过这个,我解决了这个问题。经过几个小时的挖掘,我解决了这个问题。对于任何使用LINQtoSQL创建sql数据库并为其创建模型类的人来说,这就是如何让delete工作的方法
public ActionResult Delete(int id)
{
var something = friendsDB.Friends.Single(a => a.Id == id);
return View(something);
}
[HttpPost]
public ActionResult Delete(int id, string confirmButton)
{
var sigh = friendsDB.Friends.Single(a => a.Id == id);
try
{
friendsDB.Friends.DeleteOnSubmit(sigh);
friendsDB.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
使用此选项,创建强类型删除视图 friendsDB是ObjectContext实例吗?已解决。我如何将一个问题标记为已解决lol。我没有足够的声誉来编辑,但在你上面的帖子中,你说大多数请求都是GET的,除非提交一份表格,然后它们就会被放入。也就是说,如果他们提交表单,他们就是POST。是的,我知道大多数受支持的http请求都是POST和get,我只是想试试运气。有人能帮我在我的模型类中定义一个delete方法吗。但是谢谢你,我很高兴看到它是正确的!!