Umbraco form post - using CurrentUmbracoPage with anchor tag

989 Views Asked by At

Using Umbraco 7 - I would like to perform a form post, but then return the user back to the current form if the model state fails validation.

However the application is being developed as a single page application - and with that in mind - would like to include an anchor tag in the URL so the user sees the form with validation messages.

However if I use CurrentUmbracoPage() - validation works and model-state is maintained - but I can't find a way of adding an anchor tag. If I use a RedirectResult, I lose my model state!

At present, if the form fails validation - the page reloads but will throw the user to the top of the page.

Any help? My form post and get actions are below:

    public ActionResult GetQuestionForm()
    {
        // Controller code omitted
        return PartialView("_AskAQuestionForm", model);
    }

    [HttpPost]
    public ActionResult ProcessQuestionForm(QuestionForm model)
    {
        if (!ModelState.IsValid)
        {
            if (TempData.ContainsKey(QUESTION_FORM_TEMP_DATA_KEY))
                TempData.Remove(QUESTION_FORM_TEMP_DATA_KEY);

            TempData[QUESTION_FORM_TEMP_DATA_KEY] = model;

            // I need to add #js-ask-question__answers to this but can't :(
            return CurrentUmbracoPage();
        }

        return Redirect(Umbraco.AssignedContentItem.Url + "?qf=y#js-ask-question__answers");
    }
1

There are 1 best solutions below

0
Richard Read On

Turns out if you put your model in temp data, you can retrieve it in the GET action and then re-validate it. This in turn replenishes ModelState, and allows you to use a RedirectResult instead - with anchor tags