c# - ASP.NET CORE MVC 发布表单与控制器中的 FormCollection 模型
问题描述
<div>
<form id="frm_Index" asp-controller="Index" asp-action="SaveIndexData"
data-ajax-begin="onBegin" data-ajax-complete="onComplete"
data-ajax-failure="OnFailure" data-ajax-success="OnSaveSuccess_prop"
data-ajax="true" data-ajax-method="POST">
// controls inside
<button id="btnSave" onclick=" CheckForm(event)" type="button" class="btn btn-primary">Save</button>
</form>
</div>
当我单击 btnSave 按钮时,它不会发布到控制器。500内部服务器错误。
SaveIndexData(FormCollection 集合)不起作用
[HttpPost]
public async Task<JsonResult> SaveIndexData(FormCollection collection)
{
// json result
string result = "";
// logic code
return Json(result);
}
解决方案
从 FormCollection 更改为 IFormCollection,它适用于 btnsave 的 ajax 调用和 javascript 函数 CheckForm(event) ajax 调用
[HttpPost]
public async Task<JsonResult> SaveIndexData(IFormCollection collection)
{
// json result
string result = "";
// logic code
return Json(result);
}
推荐阅读
- github - 如何在 PR 上运行测试,与 Travis CI 合并部署
- sql - 面试题:如何在列级别获得最近3个月的聚合?
- python - 所有的项目都没有被字典收录。只取最后一项。如何将所有值存储在字典中?
- angular - 当在 Angular 7 中对另一个组件进行更改时如何更新一个组件中的数据
- c# - 如何仅在自适应卡中处理空用户输入
- sql - PostgreSQL 为什么通过函数传递参数比临时表快 3 倍
- batch-file - 批处理 - For 循环中的可变子字符串,具有可变范围
- reactjs - 从子组件函数接收参数
- c++ - 当我更改其他 Mat 对象时,Mat 值会更改
- c# - 如何验证 Apigee Edge 从 .NET/C# 代码生成的 JWT 令牌?