2017-09-06 164 views
-1

我收到以下錯誤An unhandled exception occurred while processing the request. InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormCollectionModelBinder' cannot bind to a model of type 'Microsoft.AspNetCore.Http.FormCollection'. Change the model type to 'Microsoft.AspNetCore.Http.IFormCollection' instead.C#ASP.NET核心不能結合型「Microsoft.AspNetCore.Http.FormCollection」

這是當我使用下面的代碼模型:

[ValidateAntiForgeryToken] 
[HttpPost] 
public IActionResult Index(Test test, FormCollection formCollection) 
{   
    var feesAmountArray = new List<string>(); 

    foreach (var item in formCollection.Keys.Where(k => k.StartsWith("FeesAmount-"))) 
    { 
     feesAmountArray.Add(formCollection[item].ToString().TrimEnd(',')); 
    } 

    var feesAmount = string.Join(",", feesAmountArray); 

    if (ModelState.IsValid) 
    { 
    } 

    return View(test); 
} 

在模型Test內我使用了[Decimal]屬性,它與ModelBinder一起使用,但我不想綁定到表單,我只是想綁定到模型,所以我有點困惑爲什麼這個消息正在呈現。

與該模型綁定器的代碼,可以發現:

C# ASP.NET Core ModelBinder not Updating Model

任何幫助,將不勝感激:-)

+0

如果您不希望它成爲模型綁定的一部分,您希望進入「formCollection」值? –

+0

主窗體綁定到'Test'模型,但窗體可以包含由jQuery創建的一些動態字段,所以我主要需要驗證基礎'Test'模型並另外檢查生成的動態字段,這些字段不存在於'測試'模型。 – iggyweb

+0

我已經添加了額外的代碼來解決FormCollection的需求。 – iggyweb

回答

1

兩個選項:

  1. 你的參數更改爲be IFormCollection
  2. 刪除它,而是通過HttpContext.Form訪問它
+0

選項1工作,謝謝。 – iggyweb