简体   繁体   中英

One controller, multplie views, multiple forms

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM