Ad
How To Create Alert Confrim Yes/No With MvcHtmlString In MVC C#
The following code displays a warninng via MvcHtmlString.
in C#
public static MvcHtmlString ShowAlert(this HtmlHelper helper, string message)
{
StringBuilder sb = new StringBuilder();
sb.Append(@"$.confirm({" +
"title: false," +
"content: '" + message + "'," +
"type: 'dark'," +
"boxWidth: '45%'," +
"animation: 'RotateY',closeAnimation: 'RotateY',rtl: true," +
"typeAnimated: true," +
"buttons:" +
"{Close: {" +
"btnClass: 'btn-blue'} }});");
return MvcHtmlString.Create(sb.ToString());
}
in Script
<script type="text/javascript">
$('#bSubmit').click(event,
function() {
event.preventDefault();
@Html.ShowAlert("Test");
});
</script>
I want to display an Alert in MvcHtmlString which is Yes/No
and returns the clicked buttons so I can use the value in the script.
Ad
Answer
You could create a javascript library of common functions in your application and include the confirm dialog into it. This way you could use the return value of the confirm dialog in the script as a callback.
site.js:
function showConfirm(message, clickCallback) {
$.confirm({
title: false,
content: message,
type: 'dark',
boxWidth: '45%',
animation: 'RotateY',
closeAnimation: 'RotateY',
rtl: true,
typeAnimated: true,
buttons: {
Close: function () {
clickCallback("Close");
}
}
});
}
Names of the buttons could be put into the function as another parameter, for example:
function showConfirmWithMultipleButtons(message, buttons /* array of names */, clickCallback)
Usage:
showConfirm('Simple confirm message', function(buttonName) {
$.alert(buttonName);
});
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad