【问题标题】:MVC AJAX post sending updated model dataMVC AJAX 发布发送更新的模型数据
【发布时间】:2017-08-04 10:07:36
【问题描述】:

在更改某些属性的值后,我正在尝试使用模型数据向 Action 发送发布请求:

@{
  JsonSerializerSettings jss = new JsonSerializerSettings { 
  ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
}

<div id="contents">

    <!--Lead Stage-->
    @if (Model.LeadStagesNav != null)
    {
        for (int i = 0; i < Model.LeadStagesNav.Count; i++)
        {

            @Html.HiddenFor(a => a.LeadStagesNav[i].PermissionId)

            <div class="form-group" style="margin-bottom:10px">
                @Html.Label("Lead Stage", new { @class = "col-md-2" })
                <div style="display:inline-block;position:relative">
                    @Html.DropDownListFor(model => model.LeadStagesNav[i].Name, null, new { @class = "form-control", @style = "width:200px", onchange = "ChangeValue()" })
                </div>

                @if (ViewData["LeadStagesNav[" + i + "].LeadStatus"] != null)
                {
                        <!--Lead Status-->
                    @Html.Label("Lead Status", new { @style = "margin-left:15px;margin-right:15px" })
                    <div style="display:inline-block;position:relative">
                        @Html.DropDownListFor(model => model.LeadStagesNav[i].LeadStatus, null, new { @class = "form-control", @style = "width:200px", onchange = "ChangeValue()" })
                    </div>

                    if (ViewData["LeadStagesNav[" + i + "].LeadSubStatus"] != null)
                    {
                        @Html.Label("Lead Sub Status", new { @style = "margin-left:15px;margin-right:15px" })
                        <div style="display:inline-block;position:relative">
                            <!--Lead Sub Status-->
                            @Html.DropDownListFor(model => model.LeadStagesNav[i].LeadSubStatus, null, new { @class = "form-control", @style = "width:200px" })
                        </div>
                    }
                }

            </div>

                <!--Delete Button-->
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Delete Lead Stage"
                           onclick="document.getElementById('index').value = @i"
                           name="submit" class="btn btn-default" />
                    <input type="hidden" id="index" name="index" />
                </div>
            </div>
            }
        }

</div> 

<script type="text/javascript">

window.ChangeValue = function () {

    var model = @Html.Raw(JsonConvert.SerializeObject(Model, Formatting.Indented, jss));

    $.ajax({
        method: "POST",
        url: "/CmsPermissions/Edit",
        data: { permission: model },
        success: function (data) {
            $("#contents").html(data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
};

问题是我得到了旧的模型数据 发布到 Action 而不是 下拉选择值更改后的新数据, 有人知道吗?

【问题讨论】:

  • var model = @Html.Raw(.. 正在序列化原始模型。您需要序列化您的表单 - data: $('form').serialize(), 并将表单控件包装在 &lt;form&gt;
  • 但是如果任何if 块评估为假,这将失败,因为默认情况下DefaultModelBinder 要求集合索引器从零开始并且是连续的
  • 每次您在其中一个下拉列表中选择一个选项时都会调用该 ajax 函数,这是没有意义的。你到底想在这里做什么?

标签: javascript c# .net ajax asp.net-mvc


【解决方案1】:

那是因为您将旧模型作为数据传递

 var model = @Html.Raw(JsonConvert.SerializeObject(Model, Formatting.Indented, jss));

你需要序列化你的表单并传递给它一个例子是

function SubmitForm() {
    var data = $("#YourFormID").serialize();
    var url = "/YourURL/ACtion"
    var form = $('#policyForm')[0]
    var formdata = false;
    if (window.FormData) {
        formdata = new FormData(form);
    }
  return  $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        data: formdata ? formdata : data,
        cache: false,
        contentType: false,
        enctype: 'multipart/form-data',
        processData: false, 
        error: function () {
            $('#imgLoadingForPortal').modal('hide');
            Lobibox.notify('error', {
                size: 'mini',
                rounded: true,
                delay: false,
                position: 'center top', //or 'center bottom'
                msg: 'Something went wrong..please try again later',
                sound: false,
                delay: 5000,
            });
        }

    })

}

【讨论】:

  • 保单形式是什么?
  • @YanivJacobJacob 而不是策略表单,请输入您的表单客户端 ID
  • 我将模型作为 null 传递
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 2018-12-21
  • 1970-01-01
相关资源
最近更新 更多