Button submit not working when inside modal - c#

I have a page that has the following code:
<div class="form-control">
<input type="submit" value="Save" class="btn btn-primary"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Launch demo modal
</button>
</div>
and below the form:
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" >Save changes</button>
</div>
</div>
</div>
</div>
the input type submit " <input type="submit" value="Save" class="btn btn-primary"/> " works, however when I click on the modal button, and the modal opens, the button inside "Save Changes" does not work.
I have also changed the button inside the modal to the same code as the Save button, but it also doesn't work. it doesn't call the OnPost button.
I have also tried adding a handler " asp-page-handler="Save" " and renaming the OnPostAsync to OnPostSaveAsync but does not work.
The inspect does not execute anything on the browser also.
I'm using razor pages asp net6 and the modal code comes from the bootstrap5 docs.

Your <div class="modal"> is likely located as a direct child of <body> instead of being inside its <form>...
... but <button type="submit"> won't work unless the <button> itself is a descendant of the <form> element that should be submitted.
Fortunately, if the <button> is not a descendant of a <form>, then you can manually bind it using the <button form=""> attribute, which contains the id="" of the <form> you want to submit.
Note that the form="" attribute can also be used with all HTML form controls: on all <input> elements, <textarea>, and <select> - which allows you to work-around the fact we can't nest <form> elements.

Related

How to pass data from a modal button toggle to a modal window

I have a button which toggles a modal window i would like the button to get the current Model.ID of it item that the button is attached to then pass that Model.ID into the window i would like to do this without using JavaScript.
At the moment the first Model.ID out of all of them is passed in but i want the current one
To do this would i need to put the modal toggle in a form with Method GET then post the data back to a partial view inside the modal window.
HTML Code
<button type="button" data-bs-toggle="modal" data-bs-target="#staticBackdrop2" >Modal</button>
<div class="modal fade" id="staticBackdrop2" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="staticBackdropLabel">Delete</h1>
<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 delete #Model.ID this action cannot be reversed
</div>
<div class="modal-footer">
<div>
<form action="#Url.Action("DeleteLesson","Lesson", new{ID = Model.ID})" method="post">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger" value="Delete">Delete</button>
</form>
</div>
</div>
</div>
</div>
</div>
You can specify some custom parameter for your button, like
<button type="button" data-bs-toggle="modal" something="#Model.ID" data-bs-target="#staticBackdrop2" >Modal</button>
<div class="modal fade" id="staticBackdrop2" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="staticBackdropLabel">Delete</h1>
<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 delete #Model.ID this action cannot be reversed
</div>
<div class="modal-footer">
<div>
<form action="#Url.Action("DeleteLesson","Lesson", new{ID = Model.ID})" method="post">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger" value="Delete">Delete</button>
</form>
</div>
</div>
</div>
</div>
</div>
Now, at the event that opens the modal, you will have to find the referrer, which is the button. Its something attribute should hold your model's id.
The data entered the input fields are extracted using the ids of the respective fields using the jQuery val() method.
Next the data obtained from the input fields are concatenated into a string. This string is passed to the modal body using the html() method of jQuery.

Puppeteer.net: cant figure out the selector

I am using puppeteer in trying to find the selector for a login page.
the login html is:
<form action="/login?app=LM&var=au" method="POST" name="login-form" id="login-form">
<input type="hidden" name="_csrf" value="BQ2YoR3d-5P__6b5HfqlVOjv8YgTypKV92XA" />
<div class="form-group text-center">
<input type="text" name="username" id="login-form-username" placeholder="Username" class="form-control" required value="">
</div>
<div class="form-group text-center">
<input type="password" name="password" id="login-form-password" placeholder="Password" class="form-control" required>
</div>
<div class="text-center">
<button type="submit" class="btn btn-danger btn-lg btn-block">Login</button>
</div>
</form>
I tried #submit, #login-form-submit and several others for the selector. what would the selector for the login button be as there is no ID name?
i am using ClickAsync();
Either give your submit button an id property and use the #submit selector you previously tried:
<button id="submit" type="submit" class="btn btn-danger btn-lg btn-block">Login</button>
Or use form button[type=submit] as selector.

How to make a space between buttons in bootstrap using ml-auto?

I have two seperated buttons that have created, but they closely packed to another there is no space in between i have tried to use ml-auto. There is not much difference on the UI side and need some help around it.
<div class="form-group row">
<div class="col-xs-3 ml-auto">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Start New Course
</button>
</div>
</div>
<div class="form-group row">
<div class="col-xs-3 ml-auto">
<a class="btn btn-large btn-success" id="viewCourse" href="#Url.Action("ViewCourses", "Home")">View Grades</a>
<script type="text/javascript">
$('#ViewCourses').on('click', function (e) {});
</script>
</div>
</div>
First of all I think both of your buttons need to sit inside a .row element. Right now they are on different rows. Then you can apply a class like .mr-2 (margin right of 2 units, or increase the number if you want more) to the first button wrapper. Then you will have some spacing between your buttons.
<div class="row">
<div class="form-group">
<div class="col-xs-3 mr-2">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Start New Course
</button>
</div>
</div>
<div class="form-group">
<div class="col-xs-3">
<a class="btn btn-large btn-success" id="viewCourse" href="#Url.Action("ViewCourses", "Home")">View Grades</a>
<script type="text/javascript">
$('#ViewCourses').on('click', function (e) {});
</script>
</div>
</div>
</div>
You can see a working solution here: https://codepen.io/razvan-tudosa/pen/YzqvQVm
More info about how to space stuff you can find in the twitter bootstrap docs: https://getbootstrap.com/docs/4.5/utilities/spacing/

Render asp.net partial in Bootstrap modal

I need to display a partial (view component) in a bootbox control.
This partial receive some parameters from my current view and build a form to be submitted.
Until MVC 5 I did that rendering my partial to a string and send it back to view.
In MVC 6 I can't get this to work.
What is the correct way to do this?
After some research, I find out a way to do this, very easily. Just use the jquery's "load" function. The example bellow is using default WebApplication created by VisualStudio.
Main view code (Views/Home/About.cshtml):
<button class="btn btn-default details" >Click me</button>
<div class="modal" id="modal"></div>
#section scripts{
<script>
$(function () {
$(".details").click(function () {
$("#modal").load("Contact", function () {
$("#modal").modal();
})
});
})
</script>
}
Modal view code (Views/Home/Contact.cshtml):
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<address>
One Microsoft Way<br />
Redmond, WA 98052-6399<br />
<abbr title="Phone">P:</abbr>
425.555.0100
</address>
<address>
<strong>Support:</strong> Support#example.com<br />
<strong>Marketing:</strong> Marketing#example.com
</address>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->

keep dialog box(?) open after clicking submit button

<ul class="nav pull-right">
<li class="active">HOME</li>
<li>FEATURES</li>
<li>CHECK E-MAIL</li>
<li>TESTIMONIALS</li>
<li>CONTACT</li>
<li>Sign Up</li>
</ul>
<div id="Signup" class="modal hide fade LoginSignup" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel2">Sign Up</h3>
</div>
<div class="modal-body">
<p>#alert</p>
<form method="POST" action="" name="signupform">
<input type="text" placeholder="First Name" name="firstname">
<input type="text" placeholder="Last Name" name="lastname">
<input type="text" placeholder="E-mail" name="email">
<input type="password" placeholder="Password" name="password">
<input type="password" placeholder="Confirm Password" name="password1">
<input type="submit" name="signup" value="Sign Up" />
<label>By registering you are automatically agreeing to our Terms of Service</label>
</form>
</div>
</div>
this is from a html5 template that i downloaded somewhere, when i click sign up in the menu (the first code i posted) it opens some kind of dialog box(?) i fill the textboxes just fine and when i click submit it registers just fine and i wanted to keep that dialog box(?) open so i can show a message through the variable alert that says you registered successfully blabla.
i can get everything working including the alert but i cant keep the signup dialog open after submitting so i have to click signup on the menu again to see the alert message.
That looks like bootstrap to me. First things first, you have to change the submit button in the pop-up to something like:
<span class="btn btn-primary" onclick="submitform()">Submit</span>
Then you have to create the javascript function called "submitform" to submit the signup form data to a webservice or whatever backend you have, read back the response and display the response in the popup itself.

Categories

Resources