Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Bergman 483 posts 1132 karma points
    Oct 08, 2021 @ 22:16
    John Bergman
    0

    BeginUmbracoForm + FormCollection say use IFormCollection.. IFormCollection, dynamic, etc yields 400 errors

    I have a surfacecontroller that has a method receiving a post that takes a FormCollection as a parameter.

    The form is defined using a BeginUmbracoForm construct. with 5 or 6 fields. Eventually, this controller's post method will receive different models and basically record/email the information provided (hence the desire for a dynamic or a FormCollection as the parameter).

    During the post, I receive this error:

    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.
    

    I have tried it using a IFormCollection, with no parameters, and with a dynamic parameter. All of these yield a 400 bad request error.

    Is there a way to just receive the collection of form values? I am at somewhat of a loss because I've done the same type of thing using Umbraco8 with no issues.

    Here is the form:

    @using (Html.BeginUmbracoForm("EMailerHandler", "UtilitiesSurface")) { @Html.AntiForgeryToken()

          <div>
              <div class="form-group">
                      <label for="ContactName">Name</label>
                      <input type="text" class="form-control" id="ContactName" name="ContactName" placeholder="">
                  </div>
          </div>
    
          <div>
              <div class="form-group">
                  <label for="EMailAddress">Email Address</label>
                  <input type="text" class="form-control" id="EMailAddress" name="EMailAddress" placeholder=""> <!-- Required -->
              </div>
          </div>
    
          <div>
              <div class="form-group">
                  <label for="CellNumber">Cell Number</label>
                  <input type="text" class="form-control" id="CellNumber" name="CellNumber" placeholder="">
              </div>
          </div>
    
          <div>
              <div class="form-group">
                  <label for="Message">Message</label>
                  <textarea class="form-control" id="Message" name="Message"></textarea>
              </div>
              </div>
    
    <button type="submit" id="submitButton" name="submitButton" class="btn btn-primary">Send</button>
    

    }

    Here is the footprint for the controller method:

    public class UtilitiesSurfaceController : SurfaceController
      {
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> EMailerHandler(FormCollection form )
        {
    

    I've tried it with/without the antiforgery token with no different in behavior.

    Any ideas would be greatly appreciated;

  • klaash 7 posts 86 karma points
    Apr 19, 2022 @ 08:38
    klaash
    0

    Hi John,

    Did you ever find a solution for this? I'm running into exactly the same problem.

Please Sign in or register to post replies

Write your reply to:

Draft