ASP MVC将值传递到模态窗口 [英] ASP MVC Passing value into modal window

查看:93
本文介绍了ASP MVC将值传递到模态窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模态窗口的视图。当我点击打开模态窗口时,我想将参数 item.InstrumentId 传递到模态窗口,这样我就可以点击一个链接,将我重定向到该特定仪器的页面属于InstrumentId。我在下面成功地将InstrumentId传递到窗口,但问题是我不知道如何将该值传递到 Html.ActionLink 。关于如何进行的任何提示?

I have this view with a modal window. When I click to open the modal window, I'd like to pass the parameter item.InstrumentId into the modal window so that I can click on a link that redirects me to the page from that specific instrument that belongs to the InstrumentId. What I'm doing below successfully passes the InstrumentId into the window, but the problem is I don't know how to pass that value into the Html.ActionLink. Any hints as to how I can proceed?

   @foreach (var item in Model)
    {
        <li class="list-group-item">
            <div class="row">
                <div class="col-md-4">
                    <h4>
                        Instrument: @item.InstrumentId
                    </h4>
                </div>
                <div class="col-md-4">
                    @item.Type
                </div>
                <div class="col-md-4">
                    <!-- Button trigger modal -->
                    <button type="button" class="open-dialog btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal" data-id="@item.InstrumentId">
                        View details
                    </button>

                    <script type="text/javascript">
                        $(document).on("click", ".open-dialog", function() {
                            var modalId = $(this).data('id');
                            $(".modal-dialog #myModal").val(modalId);
                        })
                    </script>
                    <!-- Modal -->
                    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" 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">&times;</span></button>
                                    <h4 class="modal-title" id="myModal"></h4>
                                </div>
                                <div class="modal-body">
                                    @item.Message
                                    <br/> <br/> <br/>
                                    <div class="row">
                                        <div class="col-md-4">
                                            Slide: <a href="/Home/Slide" class="Button">1234500323</a>
                                        </div>
                                        <div class="col-md-4">
                                            Instrument: @Html.ActionLink(item.InstrumentId, "Instrument", new {instrumentid = item.InstrumentId})
                                        </div>
                                        <div class="col-md-4">
                                            Checked: @item.Checked
                                        </div>
                                    </div>
                                </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>
                        </div>
                    </div>
                </div>
            </div>
        </li>
    }

更新

<div>
<ul class="list-group">
    @foreach (var item in Model)
    {
        <li class="list-group-item">
            <div class="row">
                <div class="col-md-4">
                    <h4>
                        Instrument: @item.InstrumentId
                    </h4>
                </div>
                <div class="col-md-4">
                    @item.Type
                </div>
                <div class="col-md-4">
                    <!-- Button trigger modal -->
                    <button type="button" class="open-dialog btn btn-primary btn-sm" data-url="@Url.Action("Instrument", new {instrumentid = @item.InstrumentId})">
                        View details
                    </button>

                    <script type="text/javascript">
                        $(document).on("click", ".open-dialog", function() {
                            $('#details').attr('href', $(this).data('url')); // update the links url
                        });
                    </script>
                </div>
            </div>
        </li>
    }
</ul>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" 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">&times;</span></button>
            </div>
            <div class="modal-body">
                @*@item.Message*@
                <br /> <br /> <br />
                <div class="row">
                    <div class="col-md-4">
                        Slide: <a href="/Home/Slide" class="Button">1234500323</a>
                    </div>
                    <div class="col-md-4">
                        Instrument: <a id="details" href="#">Details</a>
                    </div>
                    <div class="col-md-4">
                        Checked: @*@item.Checked*@
                    </div>
                </div>
            </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>
    </div>
</div>

推荐答案

您当前为集合中的每个项目生成了一个模态,但是给它一个 id =myModal这是无效的html并且意味着数据-target =#myModal只会打开第一个。将模态移到循环外,这样你只创建一个(并删除< h4 class =modal-titleid =myModal>< / h4> 元素也具有相同的 id 属性)

You currently generating a modal for each item in your collection but giving it an id="myModal" which is invalid html and means data-target="#myModal" will only ever open the first one. Move the modal outside the loop so your create only one (and also remove the <h4 class="modal-title" id="myModal"></h4> element which also has the same id attribute)

然后将按钮html更改为

Then change the button html to

<button type="button" class=".." .. data-url="@Url.Action("Instrument", new { instrumentid = item.InstrumentId })">

并将模态中的html更改为

and change the html in the modal to

Instrument: <a id="details" href="#">Details</a>

然后将脚本更改为

$(document).on("click", ".open-dialog", function() {
    $('#details').attr('href', $(this).data('url')); // update the links url
})

旁注:你可能想要用做一些类似的事情:在模态中检查:@ item.Checked

这篇关于ASP MVC将值传递到模态窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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