我想用ajax调用绑定(bind)MVC模型,并想在部分 View 中使用onclick事件调用JS函数,但有一个错误bulkconfirm()未定义以及我如何基本上用ajax调用绑定(bind)模型这是我的部分 View ,其中当用户单击确认按钮时,应该调用此单击bulkconfrim()函数? 谢谢
@model Manual_Tag_Entry.ViewModel.ModelAccessor
<div class="modal-header">
<h3 class="modal-title" id="exampleModalLabel">Do you want to update following information?</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form id="test">
<table class="table table-striped table-bordered" width="80%">
<thead>
<tr>
<th>Tag Name</th>
<th>Tag Old Value</th>
<th>Tag New Value</th>
</tr>
</thead>
<tbody>
@if (Model.updatedDatas != null)
{
foreach (var item in Model.updatedDatas)
{
<tr>
<td>
@item.TagName
</td>
<td>
@item.OldTagValue
</td>
<td>
@item.NewTagValue
</td>
</tr>
}
}
</tbody>
</table>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="BulkConfirm()">Confirm Update</button>
</div>
@section script{
<script>
function BulkConfirm()
{
debugger;
var data=@Model.updatedDatas;
$.ajax({
type: 'POST', //GET
url: '@Url.Action("BulkUpdateConfirmation", "Home")',
data: data
});
$("#myModal").modal('hide')
}
</script>
}
最佳答案
您需要使用 Html Helpers
将 View 模型转换为 Javascript 对象:
var obj = @Html.Raw(Json.Encode(Model));
在这里您可以看到确切的解决方案:https://stackoverflow.com/a/16361388/4687359
关于javascript - 如何将MVC模型与ajax调用绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59526636/