Bootstrap Modal pass an ID is not working












0















I have a problem to pass an id in my modal.



What I'm trying to do is that by clicking on the link (which corresponds to an event) it opens the details of this event.
But I can not get the id and display nefusque the name of this event in my modal ...



I was inspired by Passing data to a bootstrap modal but the result is still negative ...



@model jak.formulaire.Models.Events

<div class="container">
@* Datatable of Events member *@
<div>
<table id="example" class="table table-hover" style="width:100%; margin-top:2%">
<thead>
<tr>
<th scope="col">Event Name</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Status</th>

</tr>
</thead>
</table>
</div>
</div>

@* Modal Details *@
<div class="modal" role="dialog" id="myModal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Details</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">

<div id="myModalContent">
<form id="myForm">
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Event_Name, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.Event_Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Name", @id = "Name" } })
</div>
@Html.ValidationMessageFor(model => model.Event_Name, "", new { @class = "text-danger" })
</div>
</div>
</form>
</div>


<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>


@section Scripts{

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

<script>
$(document).ready(function () {
$('#example').DataTable({

ajax: {
"url": '@Url.Action("GetHistoric", "Events")',
"dataSrc": "",
"type": "GET",
"datatype": "json"
},
columns: [
{
'data': 'Name', "render": function (data) {
return '<a href="#" class="open-Details" data-toggle"modal" data-id="' + data + '" id="' + data + '">' + data + '</a>';
},
},
{ 'data': 'Date_Begin_cU' },
{ 'data': 'Date_End_cU' },
{ 'data': 'Status' },

],
"paging": false,
"info": false,
"searching": false
});
$(document).on("click", ".open-Details", function () {
$('#Name').val(this.id);
$('#myModal').modal('show');
});


});
</script>
}









share|improve this question

























  • You may find the example here for datatables helpful: stackoverflow.com/questions/45368022/…

    – Zim
    Nov 27 '18 at 13:33
















0















I have a problem to pass an id in my modal.



What I'm trying to do is that by clicking on the link (which corresponds to an event) it opens the details of this event.
But I can not get the id and display nefusque the name of this event in my modal ...



I was inspired by Passing data to a bootstrap modal but the result is still negative ...



@model jak.formulaire.Models.Events

<div class="container">
@* Datatable of Events member *@
<div>
<table id="example" class="table table-hover" style="width:100%; margin-top:2%">
<thead>
<tr>
<th scope="col">Event Name</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Status</th>

</tr>
</thead>
</table>
</div>
</div>

@* Modal Details *@
<div class="modal" role="dialog" id="myModal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Details</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">

<div id="myModalContent">
<form id="myForm">
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Event_Name, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.Event_Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Name", @id = "Name" } })
</div>
@Html.ValidationMessageFor(model => model.Event_Name, "", new { @class = "text-danger" })
</div>
</div>
</form>
</div>


<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>


@section Scripts{

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

<script>
$(document).ready(function () {
$('#example').DataTable({

ajax: {
"url": '@Url.Action("GetHistoric", "Events")',
"dataSrc": "",
"type": "GET",
"datatype": "json"
},
columns: [
{
'data': 'Name', "render": function (data) {
return '<a href="#" class="open-Details" data-toggle"modal" data-id="' + data + '" id="' + data + '">' + data + '</a>';
},
},
{ 'data': 'Date_Begin_cU' },
{ 'data': 'Date_End_cU' },
{ 'data': 'Status' },

],
"paging": false,
"info": false,
"searching": false
});
$(document).on("click", ".open-Details", function () {
$('#Name').val(this.id);
$('#myModal').modal('show');
});


});
</script>
}









share|improve this question

























  • You may find the example here for datatables helpful: stackoverflow.com/questions/45368022/…

    – Zim
    Nov 27 '18 at 13:33














0












0








0








I have a problem to pass an id in my modal.



What I'm trying to do is that by clicking on the link (which corresponds to an event) it opens the details of this event.
But I can not get the id and display nefusque the name of this event in my modal ...



I was inspired by Passing data to a bootstrap modal but the result is still negative ...



@model jak.formulaire.Models.Events

<div class="container">
@* Datatable of Events member *@
<div>
<table id="example" class="table table-hover" style="width:100%; margin-top:2%">
<thead>
<tr>
<th scope="col">Event Name</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Status</th>

</tr>
</thead>
</table>
</div>
</div>

@* Modal Details *@
<div class="modal" role="dialog" id="myModal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Details</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">

<div id="myModalContent">
<form id="myForm">
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Event_Name, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.Event_Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Name", @id = "Name" } })
</div>
@Html.ValidationMessageFor(model => model.Event_Name, "", new { @class = "text-danger" })
</div>
</div>
</form>
</div>


<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>


@section Scripts{

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

<script>
$(document).ready(function () {
$('#example').DataTable({

ajax: {
"url": '@Url.Action("GetHistoric", "Events")',
"dataSrc": "",
"type": "GET",
"datatype": "json"
},
columns: [
{
'data': 'Name', "render": function (data) {
return '<a href="#" class="open-Details" data-toggle"modal" data-id="' + data + '" id="' + data + '">' + data + '</a>';
},
},
{ 'data': 'Date_Begin_cU' },
{ 'data': 'Date_End_cU' },
{ 'data': 'Status' },

],
"paging": false,
"info": false,
"searching": false
});
$(document).on("click", ".open-Details", function () {
$('#Name').val(this.id);
$('#myModal').modal('show');
});


});
</script>
}









share|improve this question
















I have a problem to pass an id in my modal.



What I'm trying to do is that by clicking on the link (which corresponds to an event) it opens the details of this event.
But I can not get the id and display nefusque the name of this event in my modal ...



I was inspired by Passing data to a bootstrap modal but the result is still negative ...



@model jak.formulaire.Models.Events

<div class="container">
@* Datatable of Events member *@
<div>
<table id="example" class="table table-hover" style="width:100%; margin-top:2%">
<thead>
<tr>
<th scope="col">Event Name</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Status</th>

</tr>
</thead>
</table>
</div>
</div>

@* Modal Details *@
<div class="modal" role="dialog" id="myModal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Details</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">

<div id="myModalContent">
<form id="myForm">
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Event_Name, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.Event_Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Name", @id = "Name" } })
</div>
@Html.ValidationMessageFor(model => model.Event_Name, "", new { @class = "text-danger" })
</div>
</div>
</form>
</div>


<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>


@section Scripts{

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

<script>
$(document).ready(function () {
$('#example').DataTable({

ajax: {
"url": '@Url.Action("GetHistoric", "Events")',
"dataSrc": "",
"type": "GET",
"datatype": "json"
},
columns: [
{
'data': 'Name', "render": function (data) {
return '<a href="#" class="open-Details" data-toggle"modal" data-id="' + data + '" id="' + data + '">' + data + '</a>';
},
},
{ 'data': 'Date_Begin_cU' },
{ 'data': 'Date_End_cU' },
{ 'data': 'Status' },

],
"paging": false,
"info": false,
"searching": false
});
$(document).on("click", ".open-Details", function () {
$('#Name').val(this.id);
$('#myModal').modal('show');
});


});
</script>
}






javascript asp.net-mvc bootstrap-4 asp.net-ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 13:06







Korpin

















asked Nov 27 '18 at 12:00









KorpinKorpin

7810




7810













  • You may find the example here for datatables helpful: stackoverflow.com/questions/45368022/…

    – Zim
    Nov 27 '18 at 13:33



















  • You may find the example here for datatables helpful: stackoverflow.com/questions/45368022/…

    – Zim
    Nov 27 '18 at 13:33

















You may find the example here for datatables helpful: stackoverflow.com/questions/45368022/…

– Zim
Nov 27 '18 at 13:33





You may find the example here for datatables helpful: stackoverflow.com/questions/45368022/…

– Zim
Nov 27 '18 at 13:33












1 Answer
1






active

oldest

votes


















2














You have :



data-id="data" id="' + data + '"


written in your jquery code while you are trying to fetch id from data attribute i.e.:



$('#Name').val($(this).data('id'));


which should be like:



data-id="'+data+'" id="' + data + '"


so the whole line would be :



return '<a href="#" class="open-Details" data-toggle"modal" data-id="'+data+'" id="' + data + '">' + data + '</a>';


Now the following:



$('#Name').val($(this).data("id"));


would work fine.



either you need to set data variable instead of "data" literal or use this.id like:



$('#Name').val(this.id);





share|improve this answer
























  • I just edited my initial post with edited code.

    – Korpin
    Nov 27 '18 at 12:47











  • Now i only receive "Uncaught TypeError: $(...).modal is not a function" and my modal is not opening, any guess please?

    – Korpin
    Nov 27 '18 at 13:13











  • that seems to be script error, check if all the required js files are loading properly

    – Ehsan Sajjad
    Nov 27 '18 at 13:22











  • Yup, just checked for it and it seems good...

    – Korpin
    Nov 27 '18 at 13:30











  • Okey, i had to had javascript.Noconflict to avoid this error, now i can open my modal again but my data is still not passing in my modal

    – Korpin
    Nov 28 '18 at 8:13











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499225%2fbootstrap-modal-pass-an-id-is-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You have :



data-id="data" id="' + data + '"


written in your jquery code while you are trying to fetch id from data attribute i.e.:



$('#Name').val($(this).data('id'));


which should be like:



data-id="'+data+'" id="' + data + '"


so the whole line would be :



return '<a href="#" class="open-Details" data-toggle"modal" data-id="'+data+'" id="' + data + '">' + data + '</a>';


Now the following:



$('#Name').val($(this).data("id"));


would work fine.



either you need to set data variable instead of "data" literal or use this.id like:



$('#Name').val(this.id);





share|improve this answer
























  • I just edited my initial post with edited code.

    – Korpin
    Nov 27 '18 at 12:47











  • Now i only receive "Uncaught TypeError: $(...).modal is not a function" and my modal is not opening, any guess please?

    – Korpin
    Nov 27 '18 at 13:13











  • that seems to be script error, check if all the required js files are loading properly

    – Ehsan Sajjad
    Nov 27 '18 at 13:22











  • Yup, just checked for it and it seems good...

    – Korpin
    Nov 27 '18 at 13:30











  • Okey, i had to had javascript.Noconflict to avoid this error, now i can open my modal again but my data is still not passing in my modal

    – Korpin
    Nov 28 '18 at 8:13
















2














You have :



data-id="data" id="' + data + '"


written in your jquery code while you are trying to fetch id from data attribute i.e.:



$('#Name').val($(this).data('id'));


which should be like:



data-id="'+data+'" id="' + data + '"


so the whole line would be :



return '<a href="#" class="open-Details" data-toggle"modal" data-id="'+data+'" id="' + data + '">' + data + '</a>';


Now the following:



$('#Name').val($(this).data("id"));


would work fine.



either you need to set data variable instead of "data" literal or use this.id like:



$('#Name').val(this.id);





share|improve this answer
























  • I just edited my initial post with edited code.

    – Korpin
    Nov 27 '18 at 12:47











  • Now i only receive "Uncaught TypeError: $(...).modal is not a function" and my modal is not opening, any guess please?

    – Korpin
    Nov 27 '18 at 13:13











  • that seems to be script error, check if all the required js files are loading properly

    – Ehsan Sajjad
    Nov 27 '18 at 13:22











  • Yup, just checked for it and it seems good...

    – Korpin
    Nov 27 '18 at 13:30











  • Okey, i had to had javascript.Noconflict to avoid this error, now i can open my modal again but my data is still not passing in my modal

    – Korpin
    Nov 28 '18 at 8:13














2












2








2







You have :



data-id="data" id="' + data + '"


written in your jquery code while you are trying to fetch id from data attribute i.e.:



$('#Name').val($(this).data('id'));


which should be like:



data-id="'+data+'" id="' + data + '"


so the whole line would be :



return '<a href="#" class="open-Details" data-toggle"modal" data-id="'+data+'" id="' + data + '">' + data + '</a>';


Now the following:



$('#Name').val($(this).data("id"));


would work fine.



either you need to set data variable instead of "data" literal or use this.id like:



$('#Name').val(this.id);





share|improve this answer













You have :



data-id="data" id="' + data + '"


written in your jquery code while you are trying to fetch id from data attribute i.e.:



$('#Name').val($(this).data('id'));


which should be like:



data-id="'+data+'" id="' + data + '"


so the whole line would be :



return '<a href="#" class="open-Details" data-toggle"modal" data-id="'+data+'" id="' + data + '">' + data + '</a>';


Now the following:



$('#Name').val($(this).data("id"));


would work fine.



either you need to set data variable instead of "data" literal or use this.id like:



$('#Name').val(this.id);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 12:23









Ehsan SajjadEhsan Sajjad

50.8k1068124




50.8k1068124













  • I just edited my initial post with edited code.

    – Korpin
    Nov 27 '18 at 12:47











  • Now i only receive "Uncaught TypeError: $(...).modal is not a function" and my modal is not opening, any guess please?

    – Korpin
    Nov 27 '18 at 13:13











  • that seems to be script error, check if all the required js files are loading properly

    – Ehsan Sajjad
    Nov 27 '18 at 13:22











  • Yup, just checked for it and it seems good...

    – Korpin
    Nov 27 '18 at 13:30











  • Okey, i had to had javascript.Noconflict to avoid this error, now i can open my modal again but my data is still not passing in my modal

    – Korpin
    Nov 28 '18 at 8:13



















  • I just edited my initial post with edited code.

    – Korpin
    Nov 27 '18 at 12:47











  • Now i only receive "Uncaught TypeError: $(...).modal is not a function" and my modal is not opening, any guess please?

    – Korpin
    Nov 27 '18 at 13:13











  • that seems to be script error, check if all the required js files are loading properly

    – Ehsan Sajjad
    Nov 27 '18 at 13:22











  • Yup, just checked for it and it seems good...

    – Korpin
    Nov 27 '18 at 13:30











  • Okey, i had to had javascript.Noconflict to avoid this error, now i can open my modal again but my data is still not passing in my modal

    – Korpin
    Nov 28 '18 at 8:13

















I just edited my initial post with edited code.

– Korpin
Nov 27 '18 at 12:47





I just edited my initial post with edited code.

– Korpin
Nov 27 '18 at 12:47













Now i only receive "Uncaught TypeError: $(...).modal is not a function" and my modal is not opening, any guess please?

– Korpin
Nov 27 '18 at 13:13





Now i only receive "Uncaught TypeError: $(...).modal is not a function" and my modal is not opening, any guess please?

– Korpin
Nov 27 '18 at 13:13













that seems to be script error, check if all the required js files are loading properly

– Ehsan Sajjad
Nov 27 '18 at 13:22





that seems to be script error, check if all the required js files are loading properly

– Ehsan Sajjad
Nov 27 '18 at 13:22













Yup, just checked for it and it seems good...

– Korpin
Nov 27 '18 at 13:30





Yup, just checked for it and it seems good...

– Korpin
Nov 27 '18 at 13:30













Okey, i had to had javascript.Noconflict to avoid this error, now i can open my modal again but my data is still not passing in my modal

– Korpin
Nov 28 '18 at 8:13





Okey, i had to had javascript.Noconflict to avoid this error, now i can open my modal again but my data is still not passing in my modal

– Korpin
Nov 28 '18 at 8:13




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499225%2fbootstrap-modal-pass-an-id-is-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks