javascript - Asp.NET MVC 中的模态

标签 javascript c# jquery html asp.net-mvc

我有一个 Asp.NET MVC 应用程序,我尝试使用模式来更新数据,我有两个模式用于注册和更新,注册正常,但更新不起作用,当单击时在“Alterar”按钮上,需要打开一个填充了数据的模式,我尝试使用 JavaScript 来执行此操作。

有人可以帮助我吗?

我的 table enter image description here

我的模态需要打开并填充数据 enter image description here

我的代码

<table class="table table-hover table-condensed">
    <thead>
        <tr>
            <th>ID</th>
            <th>Produto</th>
            <th>Custo</th>
            <th>Lucro</th>
            <th>Opcoes</th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null)
        {
            foreach(var sale in Model)
            {
                <tr>
                    <td>@Html.DisplayFor(model => sale.idProduct)</td>
                    <td>@Html.DisplayFor(model => sale.nameProduct)</td>
                    <td>@Html.DisplayFor(model => sale.costProduct)</td>
                    <td>@Html.DisplayFor(model => sale.profitProduct)</td>
                    <!--Get values to put on Modal-->
                    <td><a class="update" data-toggle="modal" href="#updateModal" data-id="@sale.idProduct" data-name="@sale.nameProduct" 
                           data-cost="@sale.costProduct" data-profit="@sale.profitProduct"><span class="glyphicon glyphicon-edit">Alterar</span></a></td>
                </tr>
            }
        }
    </tbody>
</table>
    enter code here

JavaScript

<script type="text/javascript">
$(document).ready(function () {
    $('.update').on('click', function () {
        var $this = $(this);
        var idProduct = $this.data('id');
        var nameProduct = $this.data('name');
        var costProduct = $this.data('cost')
        var profitProduct = $this.data('profit')
        $('#nameProduct').text(nameProduct);
        $('#costProduct').text(costProduct);
        $('#profitProduct').text(profitProduct);
        $("#updateModal #form_update #idProduct").val(idProduct);
        $('#updateModal').modal();
    });
});

模态

<div id="updateModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dissmiss="modal">&times;</button>
                <h4 class="modal-title">Alterar Produto<span id="name_product"></span></h4>
            </div>
            <form method="post" id="form_update" action="/Dashboard/Product">
                <div class="modal-body">
                    <div class="form-group">
                        <input type="hidden" name="idProduct" id="idProduct" class="form-control" placeholder="ID do produto"/>
                        <input type="text" name="nameProduct" id="nameProduct" class="form-control" placeholder="ID do produto" required />
                        <input type="text" name="costProduct" id="costProduct" class="form-control" placeholder="Custo do Produto" required />
                        <input type="text" name="profitProduct" id="profitProduct" class="form-control" placeholder="Lucro do Produto" required />
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="submit" class="btn btn-warning" value="Atualizar" />
                    <button type="button" class="btn btn-warning" data-dissmiss="modal">Cancelar</button>
                </div>
            </form>
        </div>
    </div>
</div>

最佳答案

要更改输入的值,您需要使用 val() :

<script type="text/javascript">
$(document).ready(function () {
    $('.update').on('click', function () {
        var $this = $(this);
        var idProduct = $this.data('id');
        var nameProduct = $this.data('name');
        var costProduct = $this.data('cost')
        var profitProduct = $this.data('profit')
        $('#nameProduct').val(nameProduct);
        $('#costProduct').val(costProduct);
        $('#profitProduct').val(profitProduct);
        $("#updateModal #form_update #idProduct").val(idProduct);
        $('#updateModal').modal();
    });
});

关于javascript - Asp.NET MVC 中的模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44198207/

相关文章:

c# - 扩展成员(member)提供者

javascript - 之前调用 jQuery 时未清除的选项

jquery - 自动滑动 JQUERY

javascript - 更新不相关的 Vue.js 变量导致模板中的输入值消失

javascript - 如何检查字符串是否包含Javascript中数组的所有元素

javascript - math.round 并使用 jQuery 在页面上随机移动 div

c# - 处理 TcpClient 时何时使用异步?

c# - 什么时候适合使用 KnownType 属性?

javascript - jquery.ui 工具提示 : Show tooltip only after hover for a duration of time

javascript - 为什么这段代码使用<function>.call()?