Linq to sql 在 asp.net mvc 3 中进行多次调用

发布于 2024-10-24 03:17:52 字数 1473 浏览 1 评论 0原文

当我使用 linq to sql 更新记录时,我的 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.ActionLink("Delete", "Delete", 
                                     new { id = item.lessonID }, 
                                     new AjaxOptions {  

                                                       HttpMethod = "POST",
                                                       UpdateTargetId = @rowNumber.ToString()
                                     }
                                    )

有什么想法吗?

编辑

如果我在ajax选项中使用确认=“你想删除”我将不得不单击“确定”三次。

When I am updating a record with linq to sql my DeleteLesson() method is getting called multiple times.

My controller looks like this :

    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();
            }
    }

And my view looks like this

 @Ajax.ActionLink("Delete", "Delete", 
                                     new { id = item.lessonID }, 
                                     new AjaxOptions {  

                                                       HttpMethod = "POST",
                                                       UpdateTargetId = @rowNumber.ToString()
                                     }
                                    )

Any Ideas?

EDIT

also if I use confirm = "Do you want to delete" in ajax options I will have to click okay three times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-10-31 03:17:52

您是否同时收到帖子和电话?
尝试将其

public ActionResult  Delete(int id)

具体化为

[HttpPost()]
public ActionResult  Delete(int id)

http post IMO。您真的希望用户浏览到 /Lesson/Delete/5 并让它正常工作吗?我认为您只希望从显示他们要删除的课程的回发中处理此问题。

Are you getting both a post and a get call to it?
try making

public ActionResult  Delete(int id)

into

[HttpPost()]
public ActionResult  Delete(int id)

specific to http post IMO. Do you really want users browsing to /Lesson/Delete/5 and have it work anyway? I'd think you only want this to handle from a postback where you're displaying the lesson they're going to delete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文