首页 > 解决方案 > 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);
}

标签: c#asp.net-core.net-core

解决方案


从 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);
    }

推荐阅读