gpt4 book ai didi

javascript - 传递给 ActionResult 时 model 为 null

转载 作者:行者123 更新时间:2023-11-28 16:56:27 24 4
gpt4 key购买 nike

任何人都可以发现为什么我的模型在传递给 Controller ​​时将为空吗?我在 ajax 调用中放置了一个警报来检查那里的值,它看起来是正确的,但 Controller ActionResult 上第一行的断点显示它为空。

提前致谢

ajax调用

function DeleteFromList(_id) {
var _model = $('#createLPathForm').serialize();
alert(_model);
event.preventDefault();
$('#createLPathForm').validate();
if $('#createLPathForm').validate()) {
var request = $.ajax({
type: "POST",
url: "/umbraco/Surface/CreateLPath/DeleteItems",
dataType: 'json',
data: { 'model': _model, 'id': mId },
success: function (data) {
$("#lpPartial").html(data);
},
error: function (data) {
//$('#failModal').removeClass("d-none").addClass("d-block");
}
})
}
}

Controller

[HttpPost]
[ActionName("DeleteItems")]
public ActionResult ("DeleteItems")](CreateLPathModel _model, string id)
{
List<ModuleItems> items = _model.SelectedModuleList;
ModuleItems itemToDelete = new ModuleItems();
foreach (var item in items)
{
if (item.ArticleGuid == id)
{
itemToDelete = item;
}
}
_model.SelectedModuleList.Remove(itemToDelete);
itemToDelete.isSelected = false;
_model.SelectModulesList.Add(itemToDelete);

foreach (var key in ModelState.Keys.Where(m => m.StartsWith("SelectModulesList")).ToList())
ModelState.Remove(key);

foreach (var key in ModelState.Keys.Where(m => m.StartsWith("SelectedModuleList")).ToList())
ModelState.Remove(key);

return PartialView("~/Views/Partials/LearningPaths/_CreateLPath.cshtml", _model);
}

最佳答案

您正在序列化表单并作为属性发送到数据模型。为了解决您的问题,您可以使用 _model 变量设置数据属性并将 mId 变量作为查询字符串发送:

function DeleteFromList(_id) {
var _model = $('#createLPathForm').serialize();
alert(_model);
event.preventDefault();
$('#createLPathForm').validate();
if $('#createLPathForm').validate()) {
var request = $.ajax({
type: "POST",
url: "/umbraco/Surface/CreateLPath/DeleteItems?id=" + mId,
dataType: 'json',
data: _model,
success: function (data) {
$("#lpPartial").html(data);
},
error: function (data) {
//$('#failModal').removeClass("d-none").addClass("d-block");
}
})
}
}

关于javascript - 传递给 ActionResult 时 model 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59029848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com