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 :-)
formCollection
value if you don't want it to be part of model-binding? – OsteomalaciaTest
model but the form can include some dynamic fields created by jQuery, so I need primarily to validate the baseTest
model and in addition check for dynamic fields generated which do not exist in theTest
model. – PreengageIFormCollection
instead ofFormCollection
? – OsteomalaciaTest
model, I wasn't using any model binding for theFormCollection
, hence the confusion. – Preengage