Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当按钮单击同一页面时,在引导模式中获取id_Javascript_Jquery_Ajax_Asp.net Core_Asp.net Core Signalr - Fatal编程技术网

Javascript 当按钮单击同一页面时,在引导模式中获取id

Javascript 当按钮单击同一页面时,在引导模式中获取id,javascript,jquery,ajax,asp.net-core,asp.net-core-signalr,Javascript,Jquery,Ajax,Asp.net Core,Asp.net Core Signalr,我有多个按钮来调用页面中的引导模型按钮是通过基于db表行的foreach循环生成的示例如果db表有两行,第一行id为1,第二行id是2,这意味着生成了两个按钮,我已经为每个按钮设置了特定的id,我希望当用户单击按钮时,引导弹出模式将打开并在模式中显示单击的按钮id 我想在同一页面中将按钮保持的id转换为模式您可以参考以下演示 视图代码 @model IEnumerable<MVC2_1Test.Models.City> @{ ViewData["Title"] = "Index";

我有多个按钮来调用页面中的引导模型按钮是通过基于db表行的foreach循环生成的示例如果db表有两行,第一行id为1,第二行id是2,这意味着生成了两个按钮,我已经为每个按钮设置了特定的id,我希望当用户单击按钮时,引导弹出模式将打开并在模式中显示单击的按钮id


我想在同一页面中将按钮保持的id转换为模式

您可以参考以下演示

视图代码

@model IEnumerable<MVC2_1Test.Models.City>

@{
ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CityName)
        </th>
        <th>

        </th>
    </tr>
</thead>
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CityName)
            </td>
            <td>
                <button data-id="@item.Id" data-toggle="modal" data-target="#myModal" class="ModalClick"> Edit </button>
            </td>
        </tr>
    }
</tbody>
工作原理


在模式中获取数据,无需重新加载页面或前往控制器获取数据,通过控制器的简单im也可以完成此操作。请回答我的问题。谢谢@Jagdish Kumar,我已经更新了我的回复并展示了它是如何工作的。在模态中获取id时未重新加载页面。您是否尝试过测试代码?如果您有任何其他问题,请共享可以重现该问题的相关代码。这在没有js的情况下完成,现在我想在不重新加载页面的情况下编辑和删除该记录。请帮忙
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Details</h4>
        </div>
        <div class="modal-body">
            <div class="form-group">
                <label class="control-label">Id:</label>
                <input type="text" name="id" id="id" />
            </div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
  @section Scripts
{
<script type="text/javascript">
    $(".btnModal").click(function () {
        var passedID = $(this).data('id');//get the id of the selected button
        $('input:text').val(passedID);//set the id to the input on the modal
    });
</script>