是否可以将数据从对象列表传递到MVC ASP.NET中的模式? [英] Is it possible to pass data from a list of objects to a modal in MVC ASP.NET?

查看:57
本文介绍了是否可以将数据从对象列表传递到MVC ASP.NET中的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将数据传递给弹出模式我的代码是这样的



但总是将列表的frist记录传递给模态。我想点击一个项目,当前项目被发送到模态



我尝试过:



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>

推荐答案

问题是你使用相同的 id 表示每个模态对话框。在HTML中, id 属性在文档中必须是唯一的。



当您尝试访问模态时ID为 exampleModal 的元素,浏览器无法知道要访问哪个元素,因此必须选择一个。你得到哪一个将取决于你的浏览器 - 听起来你的浏览器总是选择带有该ID的第一个元素。



你需要为每个元素分配一个唯一的ID模态元素。

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">...


这篇关于是否可以将数据从对象列表传递到MVC ASP.NET中的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆