Onclick event on modal with partial view is not working - asp.net-core

I'm using ASP.NET Core, and I have a modal which is opened from Partial view like this:
<button type="button" class="btn btn-primary" data-toggle="modal" id="OpenModal">Create Lines</button>
<div class="modal fade" id="AgreeModal" tabindex="-1" data-url='#Url.Action("OpenModal","RequestApproval")' aria-hidden="true">
<div class="modal-dialog modal-lg" id="ShowModal">
</div>
</div>
The partial view
#model IEnumerable<RequestCreateLineViewModel>
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<button class="btn btn-primary" type="button" onclick="testButton()">test</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
Test button function
$(document).ready(function () {
function testButton() {
console.log("here");
}
})
But I don't know why when I click the button on the modal (testButton onclick event), it's always return testButton is not defined. TestButton function is a script in parent view.
Please help, thanks in advance.

".ready()" is a function to be executed when the DOM is fully loaded.
In other words, execute the required function or method in it.
For example:
$(document).ready(function () {
test();
});
function test() {
alert("hello");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
You just need to modify your code like this:
<script>
$.ajax({
url: "#Url.Action("partialviewtest", "Test")",
type: "GET",
success: function (data) {
$("#ShowModal").html(data);
$("#AgreeModal").modal("show");
}
});
function testButton() {
console.log("here");
}
</script>

Related

Why doesn't my modal component get the right value of props?

The parent component has pass an integer to the child component - <DeleteModal>.
<DeleteModal> includes a button and a Modal. With clicking on the button the modal will be displayed asking for the confirmation. I've tested that the props is correctly passed to <DeleteModal> by print out the eventIndex when clicking on the button. However, when showing the eventIndex in the Modal it's always the smallest index (e.g. 0).
<template>
<button
type="button"
class="btn btn-close"
data-bs-toggle="modal"
data-bs-target="#removeModal"
aria-label="Remove"
#click="test"
></button>
<div class="modal fade" id="removeModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Remove Event</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">
Are you sure that you want to remove this event from this csv file?
This process cannot be resumed.
eventIndex: {{ this.eventIndex }}
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal"
#click="test"
>
Cancel
</button>
<button
type="button"
class="btn btn-primary"
data-bs-dismiss="modal"
#click="remove_event"
>
Remove
</button>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from "axios";
export default {
props: ["eventIndex"],
methods: {
test() {
console.log(this.eventIndex);
},
remove_event() {
//...
},
},
};
</script>
<style lang=""></style>

Something weird happed when I'm using Bootstrap Modal

Sorry about the title but in fact I don't know how is question form in my case, So I'm working on project With ASP MVC Core 3.1 I want to show Modal when User Press button, So I took the code copy past from bootstrap website and it showing with me like the picture and here is my code into view
<button type="button" class="btn btn-primary" data-toggle="modal" data-
target="#staticBackdrop">
</button>
<div class="modal fade" id="staticBackdrop" data-backdrop="static" tabindex="-1" role="dialog"
ria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
and here is my action
public IActionResult Statistics()
{
return View();
}
it sample but I don't know why it showing like this any one can help.
Ok guys here is the problem is my modal be into a view so it happened position problem when I appended into body it works fine. and here is the js code
$(function () {
$('#Btn1').click(function (event) {
$("#staticBackdrop").modal("show");
$("#staticBackdrop").appendTo("body");
});
});
Hu,
probably is CSS issues.
try to write this css code
.modal-backdrop {
/* bug fix - no overlay */
display: none;
}
for more info, read this article
https://weblog.west-wind.com/posts/2016/sep/14/bootstrap-modal-dialog-showing-under-modal-background

external page load in modal view + vuejs

I am trying to open an external page in bootstrap modal view.
It is working fine if the "open Modal" button is in normal div. But if the button is inside the div, which is accessed by vuejs, modal is opening but the page is not loading anymore.
here is my code
<div id="abc">
<button type="button" class="btn btn-primary" href="http://bing.com" data-toggle="modal" data-target="#myModal">
Open modal1
</button> <!-- this modal works perfectly and load bing webpage -->
</div>
<div id="products">
<div class="row">
<div v-for="product in allproducts" class="col-md-4 col-sm-12">
Price:{{product.price}}
<button type="button" class="btn btn-primary" v-bind:href="'http://bing.com'" data-toggle="modal" data-target="#myModal">
Open Modal 2
</button> <!-- on click, modal view is opening but bing webpage is not loading-->
</div>
</div>
</div>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$('button.btn.btn-primary').on('click', function(e) {
e.preventDefault();
var url = $(this).attr('href');
$(".modal-body").html('<iframe width="100%" height="100%" frameborder="0" scrolling="no" allowtransparency="true" src="'+url+'"></iframe>');
});
new Vue({
el: '#products',
data: {
allproducts : myJsonData,
deviceType:myDeviceType,
},
methods: {
submitValue: function(event){
},
},
});
</script>
Any idea?
Got solution: As I am calling the button with v-bind so, the trigger function should be inside method of vue js:
So i replaced button in this way
<input class="btn btn-primary" type="button" value="Click it!" v-on:click="selectProduct(product.id+'.png');" data-toggle="modal" data-target="#myModal"/>
In script we do not need $('button.btn.btn-primary').on('click', function(e) { .....} any more. instead write a method inside vue
selectProduct: function(id){
var url = "/abc.html?myselection="+id; //or anyother html page
$(".modal-body").html('<iframe width="100%" height="100%" frameborder="0" scrolling="no" allowtransparency="true" src="'+url+'"></iframe>');
}

How do access to vee-validate errors and clear them outside vue instance (component)

I want to clear errors after i close modal or after i send data to database and i do it outside vue instance, i have tryed vm.$validator.errors.clean(), and reset but these wont work for me, any suggestions how do access them outside component?
Vue.component('add-modal', {
template: '#add-modal-template',
mounted() {
$(document).on('hidden.bs.modal','.modal', function (e) {
$(this).remove('bs.modal');
$(this).removeData('bs.modal');
$(".modal-body input").val(" ");
$('.modal-body option:first').prop('selected',true);
console.log($(this).find('.modal-body').html());
var myBackup = $(this).find('.modal-body').html();
$('.modal-body').empty();
$('.modal-body').append(myBackup);
console.log(vm.$validator.Errors);
});
}
}
var vm = new Vue({
el: '#app',
data: {},
created: function(){
//if something is need to be loaded first
}
});
});
<script type="text/x-template" id="add-modal-template">
<!-- Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Add new deal</h5>
<button type="button" class="close no-glow" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="addDealForm" #submit.prevent="" v-on:submit.prevent>
<div class="form-group">
<label>Deal title</label>
<input type="text" name="title" class="form-control" id="Contact Person" v-validate="'required|max:15'" placeholder="Enter deal title" v-model="deal.title">
<span class="error text-danger" v-show="errors.has('title')">{{errors.first('title')}}</span><br>
<div class="modal-footer">
<button type="button" class="btn btn-outline-primary no-glow" data-dismiss="modal">Close</button>
<button class="btn btn-success no-glow">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</script>
I provided code example
this in your callback is not Vue instance. You can try below techniques, assign var self=this
Vue.component('add-modal', {
template: '#add-modal-template',
mounted() {
var self = this;
$(document).on('hidden.bs.modal', '.modal', function(e) {
$(this).remove('bs.modal');
$(this).removeData('bs.modal');
$(".modal-body input").val(" ");
$('.modal-body option:first').prop('selected', true);
console.log($(this).find('.modal-body').html());
var myBackup = $(this).find('.modal-body').html();
$('.modal-body').empty();
$('.modal-body').append(myBackup);
console.log(self.errors);
self.errors.clear()
});
}
}

Autofocus Input Element on Modal Open Bootstrap 4

I am trying to autofocus input element on bootstrap 4 modal open . I have tried below code from http://getbootstrap.com/docs/4.0/components/modal/ but this is not working.
Can someone help me to give working demo
$('#myModal').on('shown.bs.modal', function () {
$('#myInput').trigger('focus')
})
Try the snippet below. Perhaps you need $(document).ready()
$(document).ready(function() {
$('#myModal').on('shown.bs.modal', function() {
$('#myInput').trigger('focus');
});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="text" class="form-control" id="myInput">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
I am also use this code
<script>
function setFocus(){
$('#searchModal').on('shown.bs.modal', function () {
$('#q').focus();
});
}
</script>
Where #searchModal is modal div ID and #q is input element ID
Use this code in button
<button type="button" onclick="setFocus()">Open Modal</button>