【发布时间】: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(),并将表单控件包装在<form> -
但是如果任何
if块评估为假,这将失败,因为默认情况下DefaultModelBinder要求集合索引器从零开始并且是连续的 -
每次您在其中一个下拉列表中选择一个选项时都会调用该 ajax 函数,这是没有意义的。你到底想在这里做什么?
标签: javascript c# .net ajax asp.net-mvc