I am getting the following error 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.
This is when I use the following code:
[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);
}
Within the model Test
I am using a [Decimal]
attribute which is used in conjunction with a ModelBinder
, but I am not wanting to bind to the form in anyway, I just want to bind to the model, so I am a little confused as to why this message is presenting itself.
The code relating to the ModelBinder can be found at:
C# ASP.NET Core ModelBinder not Updating Model
Any help would be much appreciated :-)
Two options: