如何在ActionLink基于验证之前给出用户确认消息

时间:2010-05-16 03:19:18

标签: jquery html asp.net-mvc onclick url.action

我有以下链接。点击后,我想查看item.primary_company字段,如果已填充,请向用户发出警告并询问他们是否愿意继续。我怎么能这样做?

<a href="<%= Url.Action("Activate", new {id = item.company_id}) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>

修改

我已经改为这个,但点击时没有任何反应。另外,我不知道如何引用该项来检查primary_company字段。我只想留言显示item.primary_company.HasValue。我还想在确认消息中显示item.company1.company_name。

<a href="#" onclick="return Actionclick("<%= Url.Action("Activate", new {id = item.company_id}) %>");" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>


<script type="text/javascript">
function Actionclick(url)
{
    alert("myclick");
    if ( confirm('Do you want to activate this company\'s primary company and all other subsidiaries?'))
        {
            location.href(url);
        }

};
</script>

2 个答案:

答案 0 :(得分:1)

<a onclick="return companyClick(\"<%= Html.Encode(item.company_name) %>\");" href="<%= Url.Action("Activate", new { id = item.company_id }) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all">
    <span class="ui-icon ui-icon-refresh"></span>
</a>

并在javascript中:

function companyClick(companyName) {
    return confirm(
        'Do you want to activate '
        + companyName + 
        ' company's primary company and all other subsidiaries?');
}

使用jQuery:

<a href="<%= Url.Action("Activate", new { id = item.company_id }) %>" title="<%= Html.Encode(item.company_name) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all">
    <span class="ui-icon ui-icon-refresh"></span>
</a>

$(function() {
    $('a').click(function() {
        return confirm(
            'Do you want to activate '
            + $(this).title + 
            ' company's primary company and all other subsidiaries?');
    });
});

更新:

忘记逃避company's撇号:

function companyClick(companyName) {
    return confirm(
        'Do you want to activate '
        + companyName + 
        ' company\'s primary company and all other subsidiaries?');
}

答案 1 :(得分:1)

您编辑的示例中的代码因双重引用而失败。

如果item.primary_company.HasValue为真,则只显示公司名称的确认,可以在服务器端或客户端进行。

服务器端,根据状态更改链接的工作方式:

<% if (item.primary_company.HasValue) { %>
<a href="#" onclick="return Actionclick('<%= Url.Action("Activate", new {id = item.company_id}) %>', '<%= Html.Encode(item.company1.company_name) %>');"
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>
<% } else { %>
<a href="<%= Url.Action("Activate", new {id = item.company_id}) %>"
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh">
        link</span></a>
<% } %>

<script type="text/javascript">
    function Actionclick(url,companyName) {
        if (confirm('Confirm. CompanyName = ' + companyName)) {
            location.href = url;
        }
    };
</script>

客户端,向javascript发送参数,告诉它是否确认:

<a href="#" onclick="return Actionclick('<%= Url.Action("Activate", new {id = item.company_id}) %>', '<%= Html.Encode(item.company1.company_name) %>', <%= item.primary_company.HasValue ? "true" : "false" %>));"
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>

<script type="text/javascript">
    function Actionclick(url,companyName,showConfirmation) {
        if (showConfirmation) {
            if (confirm('Confirm. CompanyName = ' + companyName)) {
                location.href = url;
            }
        }
        else {
            location.href = url;
        }
    };
</script>