简体   繁体   English

如何使用ajax调用绑定MVC模型?

[英]how to bind a MVC model with ajax call?

I want to bind an MVC model with ajax call and want to call a JS function using onclick event in partial view but there is an error bulkconfirm() is not defined as well as how I can bind the model with ajax call basically this is my partial view in which when user click confirm button then this click bulkconfrim() function should be called?我想用 ajax 调用绑定一个 MVC 模型,并想在局部视图中使用 onclick 事件调用一个 JS 函数,但是有一个错误,bulkconfirm() 没有定义,以及我如何用 ajax 调用绑定模型基本上这是我的部分视图,当用户单击确认按钮时,应调用此单击 bulkconfrim() 函数? Thanks谢谢

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

You need to convert your view model into a Javascript object, using Html Helpers :您需要使用Html Helpers将您的视图模型转换为 Javascript 对象:

var obj = @Html.Raw(Json.Encode(Model));

Here you can see the exact solution: https://stackoverflow.com/a/16361388/4687359在这里你可以看到确切的解决方案: https : //stackoverflow.com/a/16361388/4687359

To add onto A. Nadjar's answer, in your view, change the for each loop in your view to a for loop and use the HTML Helpers DisplayFor and the HiddenFor for each property in your list.要添加 A. Nadjar 的答案,在您的视图中,将视图中的 for each 循环更改为 for 循环,为列表中的每个属性使用 HTML Helpers DisplayForHiddenFor。

for (var i = 0; i < Model.updatedDatas.Count; i++)
{
    <tr>
         <td>
              @Html.DisplayFor(modelitem => modelitem.updatedDatas[i].TagName)
              @Html.HiddenFor(modelitem => modelitem.updatedDatas[i].TagName)
         </td>
    </tr>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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