Copied to clipboard

Flag this post as spam?

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


  • Kyle Eck 130 posts 280 karma points
    Jun 01, 2018 @ 12:48
    Kyle Eck
    0

    Umbraco MVC On form submit redirect to another form with prefilled value

    All,

    I am tryign to redirect from one form to another and have everything correct except when I redirect to the second form i think the HTTP POST is submitting the form again on page load so that means that my validation is coming up and is not correct.

    My first forms action result where I redirect to the second form taking TempData for passing from controller to controller

    public ActionResult FooterFindADistributorSubmit(FindADistributorFormModel model)
        {
            var distributorPage = Umbraco.TypedContentAtRoot().DescendantsOrSelf<IPublishedContent>().Where(x => x.DocumentTypeAlias == "findADistributor").First();
            var distId = Convert.ToInt32(distributorPage.Id);
    
            if (ModelState.IsValid)
            {
                TempData["distZip"] = model.ZipCode;
                return RedirectToUmbracoPage(distId);
            }
            return CurrentUmbracoPage();
        }
    

    My second controller that receives the TempData

    public ActionResult RenderFindADistributorFormFull(FindADistributorFormModel model)
        {
            model.ZipCode = TempData["distZip"].ToString();
            return PartialView(LAYOUT_DIRECTORY + "_FindADistributorForm.cshtml", model);
        }
    

    My second View that should be filling the value

    @using (Html.BeginUmbracoForm("FindADistributorSubmit", "FindADistributor", FormMethod.Post)){
    
    @Html.ValidationSummary();
    
    <p class="dist-sentance">Enter a Zip Code</p>
    @Html.TextBoxFor(model => model.ZipCode, new { @class = "lbtf-input findAdistributor", @value = Model.ZipCode })
    
    <input type="submit" value="Search" class="button green solid dist-search">}
    

    Here is what my second view looks like after I redirect with the correct value enter image description here

    Why is the form being validated when all I am doing is passing a value through TempData?

    Does the RedirectToUmbracoPage do an HTTP POST and then another one when the page loads?

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Jun 02, 2018 @ 15:14
    Paul Seal
    0

    Hi Try putting [HttpGet] or [HttpPost] above your action results to specify which should run on the get or the post. This should eliminate unexpected behaviour.

    Kind regards

    Paul

  • Kyle Eck 130 posts 280 karma points
    Jun 04, 2018 @ 12:14
    Kyle Eck
    0

    Paul,

    thanks for the reply - did try specifying a GET for the form that was receiving the value and a POST For the form that was sending the value however the main form (the one in the screenshot) still validates the value before hitting the search button.

    I am out of ideas but this did appear to alleviate some other headaches i had...

    Any ideas?

  • Harry Spyrou 212 posts 604 karma points
    Jun 04, 2018 @ 13:44
    Harry Spyrou
    0

    If I'm correct you're saying that your form submits twice?

    So, the breakpoints get hit twice?

    Or does it validate twice? (if so, how do you know?)

    I'm asking because I had problems with that recently and I found a solution after a while so I can maybe help.

  • Kyle Eck 130 posts 280 karma points
    Jun 04, 2018 @ 13:54
    Kyle Eck
    0

    It only hits the breakpoint once - basically my workflow is this:

    1. User submits a form from the footer
      • this form has an HttpPost action result for submitting the form
    2. The input is captured via TempData and then saved into my model, and redirects to the umbraco page i need it too

    3. The controller for the main form then takes the value and places it into another model that has a zipcode and results public method.

    4. I pass the model to the view and place the value in the appropriate html attribute for an input

      • This has an HttpGet action result for getting the data i need.

    Furthermore every time i call the main form partial I am passing a new model to it and allowing the controller to set the zipCode method if applicable.

Please Sign in or register to post replies

Write your reply to:

Draft