How to give user confirmation message before ActionLink based on validation
I have the following link. On click, I'd like to check the item.primary_company field and if populated, give the user a warning and ask if they would like to continue. How can I do this?
<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>
EDIT
I've changed to this, but nothing happens when clicked. Also, I don't know how to reference the item to do the check on the primary_company field. I only want to message to show if item.primary_company.HasValue. I'd also like to show item.company1.company_name in the confirm message.
<a href="#" onclick="return Actioncli开发者_JAVA百科ck("<%= 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>
The code in your edited example fails because of the double use of doublequotes.
Regarding only showing the confirmation with the company name if item.primary_company.HasValue
is true, it can be done either server side or client side.
Server side, change how the link works depending on the status:
<% 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>
Client side, send a parameter to javascript, telling it whether or not to confirm:
<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>
<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>
and in javascript:
function companyClick(companyName) {
return confirm(
'Do you want to activate '
+ companyName +
' company's primary company and all other subsidiaries?');
}
And using 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?');
});
});
UPDATE:
Forgot to escape company's
apostrophe:
function companyClick(companyName) {
return confirm(
'Do you want to activate '
+ companyName +
' company\'s primary company and all other subsidiaries?');
}
精彩评论