C# Linq到sql在asp.net mvc 3中进行多次调用
当我用linq将记录更新为sql时,我的C# Linq到sql在asp.net mvc 3中进行多次调用,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,当我用linq将记录更新为sql时,我的DeleteLesson()方法被多次调用 我的控制器如下所示: public ActionResult Delete(int id) { deleteLesson(id); return Content("<p style=color:red><strong>Deleted...</strong></p>"); } public voi
DeleteLesson()
方法被多次调用
我的控制器如下所示:
public ActionResult Delete(int id)
{
deleteLesson(id);
return Content("<p style=color:red><strong>Deleted...</strong></p>");
}
public void deleteLesson(int id)
{
LLDataContext storeDB = new LLDataContext();
lesson lesson = (from l in storeDB.lessons
where l.lessonID == id
select l).Single();
lesson.statusID = DELETED;
lesson.dateDeleted = DateTime.Now;
lesson.deletedByUserID = getAppUserID();
try
{
storeDB.SubmitChanges();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
storeDB.SubmitChanges();
}
}
有什么想法吗
编辑
另外,如果我在ajax选项中使用
confirm=“Do you want delete”
,我必须单击OK三次 你是不是既收到了帖子又接到了电话?试着做
public ActionResult Delete(int id)
进入
特定于http post IMO。您真的希望用户浏览到/Lesson/Delete/5并使其正常工作吗?我认为您只希望在回发中处理此问题,在回发中显示他们将要删除的课程。这实际上是一个MVC问题,如果我理解正确,与LINQ本身无关。您如何知道该方法被调用了很多次?调试器出现异常了吗?我知道它正在运行多次,原因有两个,如果我设置断点,它将命中deleteLesson()三次。它还会引发重复的行异常。根据您的代码,它不应该为单个请求多次调用该方法。因此,我认为问题出在客户端(浏览器)。你在使用AJAX吗?无论哪种方式,请检查FireBug中的Net选项卡,并确保未发出多个HTTP请求。顶行已被切断,仅将其用作上文[HttpPost]中的post。
public ActionResult Delete(int id)
[HttpPost()]
public ActionResult Delete(int id)