简体   繁体   中英

Modal in Asp.NET MVC

I have a Asp.NET MVC application, i trying to use a modal to make the update of the data, i have two modal for Register and Update, the Register is OK, but the Update is not working, when a click on the "Alterar" button, need to open a modal with the data filled, i trying to use JavaScript to do this.

Can someone help me?

My table 在此输入图像描述

My modal that need open filled with data 在此输入图像描述

My Code

Table

<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();
    });
});

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>

to change the value of input you need to use 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();
    });
});

Your modal contains only inputs.

For setting the value of input you used $('#nameProduct').text(nameProduct) and that's the wrong thing. You need the val() method.

$('#nameProduct').val(nameProduct);

.text() method is used specially for div , span , etc.

UPDATE

<td>@Html.DisplayFor(model => sale.idProduct)</td> generates a span with the follow properties: id and name have value idProduct . The problem is that you have two elements with same id (in table and in modal). id attribute must be unique in DOM .

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" class="form-control modalInput" placeholder="ID do produto"/>
                    <input type="text" class="form-control modalInput" placeholder="ID do produto" required />
                    <input type="text" class="form-control modalInput" placeholder="Custo do Produto" required />
                    <input type="text" class="form-control modalInput" 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>

JAVASCRIPT

<script type="text/javascript">
   $(document).ready(function () {
      $('.update').on('click', function () {
         var modal=$('#updateModal');
         var idInput = modal.find('input.modalInput').eq(0);
         var nameInput = modal.find('input.modalInput').eq(1);
         var costInput = modal.find('input.modalInput').eq(2);
         var profitInput = modal.find('input.modalInput').eq(3);
         idInput.val($(this).data('id'));
         nameInput .val($(this).data('name'));
         costInput.val($(this).data('cost'));
         profitInput .val($(this).data('profit'));
         modal.modal();
     });
   });
</script>

HTML

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-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>
 }

你必须使用val()。你像文本(“asda”)一样使用。你改变了这些问题的解决方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM