0

The goal is to have one page with a wizard. Each step of the wizard is a partial view containing a form. I have only one controller (Insurance) with an action for each view. The actions receive the posted data and return the viewmodel for the next step, or the viewmodel of the current step containing the error details.

The page (Index.cshtml) has partial views, rendered as @Html.Partial("~/Views/Shared/_RegistrationCode.cshtml") and the partial view itself contains a form, rendered as @using (Html.BeginForm("RegistrationCodeDetails", "Insurance", FormMethod.Post)) { and a <input type="submit" name="nextButton" value="Verder" class="btn btn-success" /> within the form to submit it.

The code works as intended up to the point where the first action returns the viewmodel for the next step (partial view _Product) using return PartialView("_Product", productViewModel);. The ActionResult is not sent to the partial view, but rendered as a full view, so the result is a partial being rendered as the only thing on the screen.

I've fiddled with @using (Ajax.BeginForm("RegistrationCodeDetails", "Insurance", new AjaxOptions { UpdateTargetId = "articleProductOutput", HttpMethod = "Post" })) { but the data is not rendered in the second wizard step partial.

Edit:

We've decided to take a different approach: One page, one controller and basically one viewmodel. The initial data is rendered right away, data depending on other steps in the wizard is retrieved using JSON and partial views.

MeanGreen
  • 3,098
  • 5
  • 37
  • 63

1 Answers1

1

Unless you mark your partial view as [ChildActionOnly], it won't load in the same page! you Partial view should look like

[ChildActionOnly]
public ActionResult _ParialView1()
{
//TODO: Add the required code here
}

//and your PartialView should be included in the main view as :

@{Html.Action("_PartialView1","Controller1");}

Thanks and hope this helps!

Adam
  • 3,815
  • 29
  • 24
  • I cannot mark the Action the first partial posts to [ChildActionOnly], because it throws an "The action '_RegistrationCode' is accessible only by a child request." error. I can't remove it either, because then the partial view is rendered again as a separate view. Could it be wrong to have an Action which accepts data from partial A and return the output for partial B? – MeanGreen Jul 21 '14 at 12:48
  • did you mark your partial view as PartialViewResult , instead of acitonresult? see SO question? http://stackoverflow.com/questions/8866920/actionlink-to-child-action – Adam Jul 21 '14 at 12:58
  • I have two actions. The first marked [ChildActionOnly], returntype PartialViewResult, returning PartialView(...). The second (which the first partial posts to) is marked [HttpPost], returntype PartialViewResult and returning PartialView(...). Still the second action does not return a partial view, or at least it's the only output rendered. – MeanGreen Jul 21 '14 at 13:44
  • I think you should add the [ChildActionOnly] also beside the [HttpPost] they both work together, a view can have multiple attributes: [HttpPost] [ChildActionOnly] public PartialViewResult view1() – Adam Jul 21 '14 at 13:47
  • Thanks for your effort, but I've tried that before and it returns "The action '_RegistrationCodeResult' is accessible only by a child request" as soon as I post the form. – MeanGreen Jul 21 '14 at 13:54