【发布时间】:2020-02-13 17:21:31
【问题描述】:
在我的项目(ASP.NET Core 2.2.MVC)中,我正在尝试向视图模型添加附加参数的 ajax 帖子。页面上的表单有一个 id='frmMain'。
我的 js 是这样的:
//post
var data = {
model: $("#frmMain").serialize(),
passtest: 'test'
};
$.ajax({
type: 'post',
url: url,
data: JSON.stringify(data),
dataType: 'json',
success: function () {
//alert('form was submitted');
}
}).done(function (result) {
if (result.status === "success") {
//some code here
}
} else {
//some code here
}
});
我的控制器操作如下所示:
public IActionResult DoSomething(MyModel model, string passtest)
{
//some action code here
}
现在,帖子有效,但在我的控制器操作中,只有“模型”填充了数据。变量“passtest”为空。 “模型”中也没有“通过测试”。
我做错了什么?
注意:我发现了一种填充隐藏字段(并将它们添加到视图模型中)的解决方法,但显然在帖子本身中传递多个参数似乎更实用...
【问题讨论】: