javascript - 如何将MVC模型与ajax调用绑定(bind)?

标签 javascript ajax asp.net-mvc

我想用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">&times;</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/

相关文章:

javascript - 将循环结构转换为 json 时出现类型错误

javascript - 更改 Slick slider 中的箭头按钮

javascript - 制作Ajax Handler,需要问一些安全问题

javascript - 在 AJAX 调用上返回 html 表单字段

jquery - 什么时候通过 AJAX 提交表单比较好,什么时候通过服务器端提交比较好?

javascript - html5 canvas.toDataURL 输出尺寸大于原始图像尺寸

javascript - JQuery - 每个 session 运行一次

c# - 授权具有多个角色的属性

c# - 将 Kendo DataSourceRequest 转换回查询字符串

javascript - 从 JavaScript 访问 ViewBag