I know it must be something stupid, but I cant seem to figure this out. I have the following in a view:
@using (@Html.BeginForm("ReceiveForm1", "Home", FormMethod.Post))
{
<div >
<label for="organizationID">Organization ID</label>
<input type="number" asp-for="organizationID" aria-describedby="emailHelp" placeholder="Enter Organization ID">
</div>
<div >
<label for="externalPersonalID">External Personal ID</label>
<input type="number" asp-for="externalPersonalID" placeholder="Enter External Personal ID">
</div>
<div >
<label for="phoneNumber">Phone Number</label>
<input type="number" id="phoneNumber" asp-for="phoneNumber" placeholder="Password">
</div>
<button type="submit" >Submit</button>
}
And I have the following code in my controller:
[HttpPost]
public ActionResult ReceiveForm1(FormCollection collection)
{
IEnumerable<Form1Return> personData = GetPersonInformation(collection);
return View(personData);
}
When I run the code, and enter data into the form, and click the submit button, I run to a breakpoint set on call to GetPersonInformation. When I do a watch on collection, there are no elements. So for some reason, the form data is not making it to the controller. Any idea why?
Thanks.
CodePudding user response:
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.
Please try this:
[HttpPost]
public ActionResult ReceiveForm1(IFormCollection collection)
{
IEnumerable<Form1Return> personData = GetPersonInformation(collection);
return View(personData);
}
CodePudding user response:
I've seen this happen when input
's don't have their name
attribute set. The relevant section on W3Schools has this nugget which confirms this is likely the case.
Note: Only form elements with a name attribute will have their values passed when submitting a form.