最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ASP.NET MVC3 Ajax.ActionLink - Conditional confirmation dialog box - Stack Overflow

programmeradmin5浏览0评论

I have an @Ajax.ActionLink which for which I would like display a confirmation dialog box only if certain conditions are met (user has unsaved changes). I created a javascript function that shows the confirmation dialog as needed, and returns true or false based on the response. I tied it into the onclick event of the ActionLink but a false result does not cancel the action. Here's a sample of my code:

@Ajax.ActionLink("Done", .. , .. , 
                  new AjaxOptions() { UpdateTargetId = "MyContainerId"},
                  new { onclick = "ConfirmDone()" })

Here's the javascript function

function ConfirmDone() {
    //for testing purposes we can always show the dialog box
    return confirm("Are you sure you want to lose unsaved changes?");
}

What's the best approach to display a conditional confirmation dialog box for the Ajax.ActionLink?

I have an @Ajax.ActionLink which for which I would like display a confirmation dialog box only if certain conditions are met (user has unsaved changes). I created a javascript function that shows the confirmation dialog as needed, and returns true or false based on the response. I tied it into the onclick event of the ActionLink but a false result does not cancel the action. Here's a sample of my code:

@Ajax.ActionLink("Done", .. , .. , 
                  new AjaxOptions() { UpdateTargetId = "MyContainerId"},
                  new { onclick = "ConfirmDone()" })

Here's the javascript function

function ConfirmDone() {
    //for testing purposes we can always show the dialog box
    return confirm("Are you sure you want to lose unsaved changes?");
}

What's the best approach to display a conditional confirmation dialog box for the Ajax.ActionLink?

Share Improve this question asked Mar 7, 2012 at 16:03 AlexAlex 9,42912 gold badges75 silver badges83 bronze badges 1
  • I would use a separate form and button as described here: stackoverflow./a/30759201/869290 – Brian Herbert Commented Jun 11, 2015 at 9:46
Add a ment  | 

1 Answer 1

Reset to default 15

Use the OnBegin event:

@Ajax.ActionLink("Done", "ActionName", 
    new AjaxOptions 
    { 
        OnBegin = "return ConfirmDone()", 
        UpdateTargetId = "MyContainerId" 
    })

You could also use the Confirm ajax option if all you need to do is pop up a confirm box. If you need to do more custom logic (or want to use a custom dialog) then you would need to use OnBegin.

Here is an example of using Confirm:

@Ajax.ActionLink("Done", "ActionName", 
    new AjaxOptions 
    { 
        Confirm= "Are you sure you want to do this?", 
        UpdateTargetId = "MyContainerId" 
    })
发布评论

评论列表(0)

  1. 暂无评论