Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to pass a data to popup modal my code is like this

but always the frist record of list passes to modal .I want to click to an item and the current item is sent to modal

What I have tried:

@for (int i = 0; i < Model.Count; i++)
       {
                                               @Model[i].Name
                                                 

                    <div class=" control-label col-lg-12">
                        
                            اضافه کردن محصول
                        
                    </div>
            
                    <div class="modal fade" id="exampleModal">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="exampleModalLabel">اضافه کردن محصول به سینی</h5>
                                    <div>
                                        
                                            <span>×</span>
                                        
                                    </div>
                                </div>
                                <div class="modal-body">

                                    @Html.Partial("_create", @Model[@i])

                                    @*
                        <span id="idHolder"></span>*@
                                </div>
                                @*<div class="modal-footer">
                        Close
                        Save changes
                    </div>*@
                            </div>
                        </div>
                    </div>
Posted
Updated 11-Sep-18 5:51am
v2
Comments
Afzaal Ahmad Zeeshan 10-Sep-18 12:54pm    
How are you displaying the modal? On a click of something, or at the page load?
Sunasara Imdadhusen 11-Sep-18 1:09am    
Can you share more insight? How you are showing the popup and also some snipped from controller.

1 solution

The problem is that you're using the same id for each modal dialog. In HTML, the id attribute needs to be unique within the document.

When you try to access the modal element with ID exampleModal, the browser has no way to know which element you want to access, so it has to pick one. Which one you get will depend on your browser - it sounds like your browser is always picking the first element with that ID.

You need to assign a unique ID to each modal element.
@for (int i = 0; i < Model.Count; i++)
{
    string modalId = "exampleModal" + i;
    string headerId = "exampleModalLabel" + i;
    
    ...
    
    <div class="modal fade" id="@modalId" tabindex="-1" role="dialog" aria-labelledby="@headerId" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="@headerId">...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900