U4-1819 - Form post in a childaction creates a loop

Created by Edwin van Koppen 01 Mar 2013, 15:19:28 Updated by Sebastiaan Janssen 04 Apr 2013, 14:22:02

Got a action class:

@Html.Action("FormContact", "Contact")

namespace Development.Controllers { public class ContactController : SurfaceController { [ChildActionOnly] public ActionResult FormContact() { return PartialView("ContactForm"); } [HttpPost] public ActionResult FormContact(ContactForm Form) { if (!ModelState.IsValid) { return CurrentUmbracoPage(); } return RedirectToCurrentUmbracoPage();
}

    public class ContactForm  {
        [Required(ErrorMessage = "Naam is verplicht")]
        [Display(Name="Naam")]
        public string Name { get; set; }
        
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Geef een valide e-mailadres")]
        [Required(ErrorMessage = "E-mail is verplicht")]
        [Display(Name = "E-mail")]
        public string Email { get; set; }

        [Display(Name = "Telefoon")]
        public string Phone { get; set; }

        [Required(ErrorMessage = "Opmerking is verplicht")]
        [Display(Name = "Opmerkingen")]
        public string Comment { get; set; }
    }
}

}

and the view:

@model Development.Controllers.ContactController.ContactForm @using (Html.BeginUmbracoForm<Development.Controllers.ContactController>("FormContact")) { @Html.ValidationSummary(true)

ContactForm

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Phone)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Phone)
        @Html.ValidationMessageFor(model => model.Phone)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Comment)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Comment)
        @Html.ValidationMessageFor(model => model.Comment)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

}

After the post with a invalid modelstate it goes to return CurrentUmbracoPage() and it does not go to FormContact() but again to FormContact(ContactForm Form), after renaming FormContact(ContactForm Form) to PostFormContact(ContactForm Form) it all works

Comments

Shannon Deminick 01 Mar 2013, 23:03:36

Yup i can replicate but is very strange. I'm not sure why @Html.Action when not redirecting would execute the method with [HttpPost] ! Investigating now.


Shannon Deminick 01 Mar 2013, 23:06:52

Looks like the same issue as this http://stackoverflow.com/questions/14036870/render-child-action-given-httpverb


Shannon Deminick 01 Mar 2013, 23:42:42

The only way to solve this issue is to create our own attributes. The reason this is happening is that MVC is choosing the [HttpPost] action with the same name because the current request has an HttpPost Verb in it (which you cannot remove because you are still in the context of an HttpPost).

So we either just stick to having different names for your child action and accepting a post or we create a new attribute specifically for this which could look like something like:

[NotChildAction] [HttpPost] public ActionResult FormContact(ContactForm Form) {

which would instruct MVC to not execute this method for a child action request ever.


Shannon Deminick 01 Mar 2013, 23:46:46

This is added and working in this changeset: 4c98295c45d1 Which is in the 6.1 branch but I'll merge it to the 4.x and 6.0.2 branch too.


Sebastiaan Janssen 04 Apr 2013, 14:22:02

Removing due in 6.1 as this was already implemented in an earlier version, which means it's included in all later versions as well.


Priority: Normal

Type: Bug

State: Fixed

Assignee: Shannon Deminick

Difficulty: Normal

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions: 4.10.0, 4.11.0, 6.0.0, 4.11.1, 4.11.2, 4.11.3, 4.11.4, 6.0.1, 4.11.5

Due in version: 4.11.6, 6.0.3

Sprint:

Story Points:

Cycle: