【问题标题】:How to get input data from MVC ViewModel into ajax post如何从 MVC ViewModel 获取输入数据到 ajax post
【发布时间】:2020-06-26 09:32:36
【问题描述】:

我正在将 SQL 数据库表中的数据提取到 ASP.NET MVC Razor 视图中,但是我无法将更改的输入数据输入到 Ajax 帖子中,例如:

@model MyViewModel
    <div class="row">
        <div class="col-1">
            @Html.LabelFor(model => model.Id, "Id:", new {})
        </div>
        <div class="col-4">
            @Html.Label(Model.Id.ToString(), new { title = "" })
        </div>
    </div>

    <div class="row">
        <div class="col-1">
            @Html.LabelFor(model => model.Test, "Test:", new { })
        </div>
        <div class="col-9">
            @Html.TextBoxFor(model => model.Test, new { data_bind = "value: Test", @type = "text" })
        </div>
    </div>

    @section scripts {
        <script type="text/javascript">
                $("#save-click").click(function () {
                    var nr = @Model.Id;
                    var postData = @Html.Raw(Json.Encode(@Model));
                    //alert(postData.Test);

                    $.ajax({
                        type: "POST",
                        url: actions.test.createOrUpdate + "?id=" + nr,
                        dataType: "json",
                        traditional: true,
                        data: postData,
                        success: function (response) {
                            if (response.Code == 0) {
                                    else {
                                        window.location.reload(false);
                                    }
                            } else {
                                alert('err');
                            }
                        }
                    });
                });
            });
        </script>
    }

当我加载视图时,一切都正确显示。控制器动作被正确触发,并且 Id(不能更改)也被正确传递。但是,当输入字段发生更改时,不是将更改后的值传递给控制器​​,而是将原始值传递到视图中。

序列化似乎有效,因为警报 (postData.Test) 返回一个值 - 但始终保持不变。

任何帮助将不胜感激。

【问题讨论】:

    标签: ajax asp.net-mvc razor-pages


    【解决方案1】:
    var postData = @Html.Raw(Json.Encode(@Model));
    

    这条线是罪魁祸首。当 Razor 渲染脚本时,它会将原始/未更改的模型分配给您的 postData 变量。

    如果您使用“检查元素”或开发工具检查 postData 的值,您会发现这些值不会更改,因为它们是静态分配的。

    您需要在每次点击按钮时检查使用

    var postData = $("form").serialize();
    

    并确保将您的输入字段包装在表单标签内。见以下代码:

    <form>
       <div class="row">
            <div class="col-1">
                @Html.LabelFor(model => model.Id, "Id:", new {})
            </div>
            <div class="col-4">
                @Html.Label(Model.Id.ToString(), new { title = "" })
            </div>
        </div>
    
        <div class="row">
            <div class="col-1">
                @Html.LabelFor(model => model.Test, "Test:", new { })
            </div>
            <div class="col-9">
                @Html.TextBoxFor(model => model.Test, new { data_bind = "value: Test", @type = "text" })
            </div>
        </div>
    </form>
    
    @section scripts {
         <script type="text/javascript">
                 $("#save-click").click(function () {
                     var nr = @Model.Id;
    
                     // use form.serialize or use the id/class of your form
                     var postData = $("form").serialize();
    
                     $.ajax({
                         type: "POST",
                         url: actions.test.createOrUpdate + "?id=" + nr,
                         dataType: "json",
                         traditional: true,
                         data: postData,
                         success: function (response) {
                             if (response.Code == 0) {
                                     else {
                                         window.location.reload(false);
                                     }
                             } else {
                                 alert('err');
                             }
                         }
                     });
                 });
             });
         </script>
    }
    

    【讨论】:

    • 完美运行!但是,此外,我想将一个值对('__RequestVerificationToken',token)附加到 postData。我尝试过的一切(.concat,.append ...)都不起作用。有什么建议吗?
    • @Manu 要添加另一个值对,只需在表单序列化的末尾添加+PropertyName=Valuevar postData = $("form").serialize()+"&amp;__RequestVerificationToken="+token;
    • 试过了,脚本部分没有错误,但控制器不接受令牌。当我将表单数据直接添加到 ajax 调用的数据中时,例如 (data: {name: value, ...} 一切正常。但是,我的表单有很多输入字段...所以我正在寻找简短的方法序列化它们。
    • 这可能会有所帮助; stackoverflow.com/questions/14473597/…
    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2011-05-10
    • 2018-07-25
    • 1970-01-01
    相关资源
    最近更新 更多