我有一個Asp.NET MVC應用程序,我試圖使用模式來更新數據,我有兩種模式的註冊和更新,註冊是好的,但更新是不工作,當點擊「Alterar」按鈕時,需要打開一個填充數據的模式,我試圖用JavaScript來做到這一點。Modal在Asp.NET MVC
有人可以幫助我嗎?
我的代碼
表
<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">×</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>
嘗試使用'$ this.attr('data-id');'而不是'$ this.data('id');' –