HTML Beginform in modal in foreach loop - html

I have a foreach loop that runs trough all my appointments. All of these appointments have to receive a rating which I would like the user to fill in inside a bootstrap modal.
I've tried passing the id to the HTTPPost method without the modal which went perfectly.
I have a feeling the data-toggle attribute is ruining it.
I know this sort of code isn't ideal but i would like the user to fill in a review without redirecting them to another page.
Is there somebody who knows how to make sure the id passed to the HTTPPost method is the correct one? Right now I always get the ID of the first appointment in the list.
Any help is more than welcome.
<tbody>
#foreach (Appointment a in Model.Appointments)
{
if (DateTime.Now > a.endingDate && Model.Account.idBabysitters.Equals(null))
{
<tr>
<td data-name="Van" class="text-center"><p>#a.startingDate</p></td>
<td data-name="Tot" class="text-center"><p>#a.endingDate</p></td>
<td data-name="Prijs" class="text-center"><p>#a.fee</p></td>
<td><button type="button" class="btn btn-primary btnReview" data-toggle="modal" data-target=".myModal">✩ Geef punten</button></td>
<td class="modalTD">
<div class="container">
#using (Html.BeginForm("Score", "Appointment", new { #id = a.idAppointments, enctype = "multipart/form-data" }, FormMethod.Post))
{
<div class="modal fade myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close btnCloseModal" data-dismiss="modal">×</button>
<h4 class="modal-title">Schrijf Recensie</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<input type="submit" value="Klaar!" class="btn btn-default btnCloseModal">
</div>
</div>
</div>
</div>
}
</div>
</td>
</tr>
}
}
</tbody>

Your btn for modal
<button type="button" class="btn btn-primary btnReview" data-toggle="modal" data-target="#(String.Format("{0} {1}", .myModal, a.idAppointments))">✩ Geef punten</button>
You Modal
<div class="#String.Format("{0} {1} {2} {3}, modal, fade, myModal, a.idAppointments")" role="dialog">
Idea is to have a different modal class name for each of your appointments item. As for now your button is always getting the first modal it can find thus you are getting the same id in the post method.

Related

Angular: Add value to input from onClick event button

I'm trying to make an Edit Modal using Angular. the td fields get a value from this table and send it to a modal using the (click) event
<tbody>
<tr *ngFor="let mascota of mascotas" class="text-center ">
<td>{{mascota.name}}</td>
<td>{{mascota.birthDate}}</td>
<td>{{mascota.species.name}}</td>
<td>
<button class="btn btn-info mx-2">Detalles</button>
<button class="btn btn-warning mx-2" (click)="openSM(contenido,mascota)">Editar</button>
<button class="btn btn-danger mx-2">Remover</button>
</td>
</tr>
</tbody>
This is the function that I made in the component, used a const in order to get the values and send it to the modal
openSM(contenido, mascota) {
const modalRef = this.modal.open(contenido,{size:'lg',centered:true});
modalRef["mascota"] = mascota;
console.log(modalRef);
}
And this is the modal where I'm tying to get the value
<ng-template #contenido let-modal>
<div class="modal-header">
<h4 class="modal-title">Editar datos de mascota</h4>
<button class="close" aria-label="close" type="button" (click)="modal.close()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="form-horizontal form-material" id="editform" autocomplete="off">
<div class="form-group m-t-10">
<div class="col-sm-10">
<label>Nombre de Mascota:</label>
<input class="form-control" type="text" placeholder="Nombre" value="mascota.name" />
</div>
</div>
</form>
</div>
</ng-template>
But when I click on the Edit button it didn't recognize the value
And when I used {{}} the const get an 'undefined' value
Error from Inspect Element
Contenido Data
Is there any way to get that value and put it into the input tag? I want to do the same with the others td fields, but first this needs to work well
Console log of the inputs in the constructor

use ngModel inside <p> tag in Angular 8

Html code:
<div class="col-md-4" *ngFor="let card of allCards">
<p class="card-text">Card Color: {{card.color}}</p>
<button type="button" class="btn btn-sm btn-primary product-btn" (click)="addCard()">Add Card</button>"
Here I also want to bind the value "card.color" into another variable (maybe using ngModel) so that I can use it inside my typescript file in angular 8.
I tried the below but not working:
<p class="card-text" [(ngModel)]="cardColor">Card Color: {{card.color}}</p>
You could send the contents to the button click event's handler.
Template (*.html)
<div class="col-md-4" *ngFor="let card of allCards">
<p class="card-text">Card Color: {{card.color}}</p>
<button
type="button"
class="btn btn-sm btn-primary product-btn"
(click)="addCard(card.color)"
>
Add Card
</button>
Controller (*.ts)
addCard(color: any) {
// use `color` here
}

Create a form for each checkbox in the foreach loop with Laravel

I am trying to create a form for each item (checkboxes) inside the foreach item and save the item to the database whenever the item is checked.
My problem is even I created a form inside the foreach loop, so every item has its own form; when I do inspect the element I find that all item are included in the same form and only the first checked element is saved to the database
here is my code in order to create a form for each item
#foreach($sessions as $session)
<h1> {{$session->quarter}} {{$session->year}} </h1>
#foreach($courses as $course)
#if($session->id== $course->quarterIDFk)
<form action="{{route('course.add')}}" method="POST" id="report" >
<div class="aa">
<div class="dd">
<button >
<section class="checkboxFour">
<input hidden type="checkbox" id="{{ $course->id}}" name="{{$course->id}}" onclick="showComboBox(id)"; />
<label for="{{ $course->id}}"></label>
</section>
</button>
<label class="class_name" value ="{{ $course->id}}" >{{ $course->courseNumber}}</label>
<input hidden type="number" value="{{ $course->id}}" name ="course"/>
</div>
<div class="select-style" id="check{{ $course->id}}" hidden >
<select name="a">
<option value="" selected disabled hidden>Taken on </option>
<option value="Summer">Summer</option>
<option value="Fall">Fall</option>
<option value="Winter">Winter</option>
<option value="Spring">Spring</option>
</select>
</div>
</div>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Modal body..
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" >Close</button>
<button type="button" class="btn btn-success" onclick="hideComboBox({{ $course->id}})" data-dismiss="modal">Yes</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
</from>
#endforeach
and i used this ajax script so the page doesn't load whenener i click the submit button
<script>
$(function () {
$('#report').submit(function (e) {
e.preventDefault() // prevent the form from 'submitting'
var url = e.target.action // get the target
var formData = $(this).serialize() // get form data
$.post(url, formData, function (response) { // send; response.data will be what is returned
// alert('report sent')
})
})
})
</script>
and here is the controller function to store checked items to the database
public function store(Request $request)
{
if(!empty($request->a))
{
$claass = new Taken_classes ;
$claass->idUser = Auth::user()->id ;
$claass->idCourse = $request->course;
// $claass->idCourse = 12;
$claass->taken_session = $request->a;
$claass->save() ; }
I expect to save each checked item to the database but just the first checked item is saved

Show Partial view in Bootstrap modal

I am creating MVC app and using Bootstrap Modal to give an opportunity to user to check input data, and to send those data to controller using Ajax. Modal window:
<div class="modal" tabindex="-1" role="dialog" id="myModal" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Check input data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#using (Ajax.BeginForm("SelectReservationDateTime", new AjaxOptions { UpdateTargetId = "divChangeRegion" }))
{
<div class="modal-body">
<table class="table">
<tr>
<td>Category name: </td>
<td><b>#Model.CategoryName </b></td>
</tr>
<tr>
<td>Date: </td>
<td><label id="SelectedDateTimeLabel"></label></td>
</tr>
<tr>
<td>Client name: </td>
<td><input type="text" name="ClientName" id="ClientName" value="#Model.ClientName" /></td>
</tr>
<tr>
<td>Client code: </td>
<td><input type="text" name="ClientCode" id="ClientCode" value="#Model.ClientCode" /></td>
</tr>
</table>
</div>
<input type="hidden" name="CategoryId" id="CategoryId" value="#Model.CategoryId" />
<input type="hidden" name="SelectedDateTime" id="SelectedDateTime" />
<div class="modal-footer" id="divChangeRegion">
<input type="submit" class="btn btn-info" value="OK" data-backdrop="static">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>
}
</div>
</div>
</div>
And after pressing the submit button I execute controller's method which save inputed data in database and send back partial view, whcih I want to input in those Modal
Controller's code:
public ActionResult SelectReservationDateTime(ReservationTimeModel PartialViewModel)
{
...
return PartialView(newModel);
}
Partial view code:
#model EQueue.Data.Ticket
<div class="row">
<div class="col-md-4 offset-md-4">
<table>
<tr>
<td class="border" id="printThis">
You get ticket №<b>#Model.CodeTicket</b><br />
Category Name <br />
<b>"#Model.TicketCategory.NameCategory"</b><br />
</td>
</tr>
<tr>
<td><input type="button" class="btn btn-primary text-center" value="Print ticket" onclick="printTicket('printThis')" data-dismiss="modal"></td>
<td><input type="button">
</tr>
</table>
</div>
</div>
<script>
function printTicket(partForPrint)
{
var printContents = document.getElementById(partForPrint).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
But I get new window instead of creating partial view in existing Modal, can anybody help me?
Firstly I would change this line:
#using (Ajax.BeginForm("SelectReservationDateTime", new AjaxOptions { UpdateTargetId = "divChangeRegion" }))
To:
#using(Html.BeginForm(null, null, FormMethod.Post, new {id = "whatever"}))
Then have some jquery that uses the id of the form to check if it's been submitted. Once it has you should fire an ajax call to that method of yours that returns a partialview. To implement a partialview you need to specify a div where it will be place using an id and then in your success use this line of code to put the partialview into that div.
$("id of the div").html(result);

updating post with bootstrap modal using ajax+laravel I've problems with json part

I'm trying to update posts with ajax and to do so, I use bootstrap modal, I previously had problems with filling my modal but I solved it by targetting elements with jquery now I'm having problems with saving and closing the modal I assume the problem caused by the .done part. Any help is appreciated.
Here is the js file where I have my code for editing:
var postId = 0;
var postBodyElement = null;
var postTitleElement = null;
$('.post').find('.edit').on('click',function(event) {
event.preventDefault();
var post = $(event.target).closest('.post');
postTitleElement = post.find('.post-title').text();
postBodyElement = post.find('.post-body').text();
var postTitle = postTitleElement;
var postBody = postBodyElement;
postId = event.target.parentNode.parentNode.dataset['postid'];
$('#post-title').val(postTitle);
$('#post-body').val(postBody);
$('#edit-modal').modal();
});
$('#modal-save').on('click', function() {
$.ajax({
method: 'Post',
url: urlEdit,
data: {title: $('#post-title').val(), body: $('#post-body').val(),postId: postId, _token: token}
})
.done(function (msg){
$(postTitleElement).text(msg['new_title']);
$(postBodyElement).text(msg['new_body']);
$('#edit-modal').modal('hide');
});
});
I'm predicting
$(postTitleElement).text(msg['new_title']);
$(postBodyElement).text(msg['new_body']);
Is where I have the problem and instead of .text I must do something else.
Also I have problems with all my bootstrap javascript components since I started working on editing.
Here is the modal:
<div class="modal fade" tabindex="-1" role="dialog" id="edit-modal">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Edit the Article</h4>
</div>
<div class="modal-body">
<form action="">
<div class="datasetpost form-group">
<label for="post-title">Title</label>
<input type="text" class="form-control" name="title" id="post-title">
<br>
<label for="post-body">Post Body</label>
<textarea class="form-control" name="post-body" id="post-body" cols="60" rows="10"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
This is where they will appear:
<div class="media-body">
<header class="post-title"><h5 class="media-heading">{{ $post->title }}<i class="fa fa-angle-down pull-right " aria-hidden="true"></i></h5> </header>
<p class="post-body"><i>{{ $post->body }}</i></p>
<div class="info">
Posted by {{ $post->user->name }} on {{ $post->created_at }}
</div>
#if (Auth::check())
<div class="interaction">
Like |
Dislike
#if (Auth::user() == $post->user)
|
Edit |
Delete
#endif
</div>
#else
Login to interract with posts!
#endif
</div>
And my guess for the the solution would be something inside .done()
Because I'm already sending .text as you can see in js file since I have these lines:
var post = $(event.target).closest('.post');
postTitleElement = post.find('.post-title').text();
postBodyElement = post.find('.post-body').text();
var postTitle = postTitleElement;
var postBody = postBodyElement;
postId = event.target.parentNode.parentNode.dataset['postid'];
$('#post-title').val(postTitle);
$('#post-body').val(postBody);
For example postBodyTitle has .text at it's ending when I assigne the .post-title
But I again try to turn it into .text or something inside .done()? and than the fun begins?