2

I want to make something like a questionnaire with different sections. Therefore, I created an Index.cshtml, which includes the according partial View (depending on the number my step-integer has). Something like

@using (Html.BeginForm())
{
        @Html.Partial("Step" + Model.Step, Model)
        <p>
            @if (Model.Step > 1)
            {
                <button name="button" value="Zurück" />
            }
            <input type="submit" value="Weiter" />
        </p>
}

As you may already noticed, It will look for something like "Step1.cshtml" and so on, where the according input mask is placed, for example

@model CVGen.Models.InputModel
<div class="editor-field">
    @Html.TextBoxFor(model => model.Anrede)
    @Html.ValidationMessageFor(model => model.Anrede)
</div>

My model contains the properties of the whole questionnaire, so of course I want to pass the whole model every time to the next step/view (and if I go back the model should be passed back too, so that the information is still there and the user can edit it). But it just won't work for me. My controller looks something like this:

public ActionResult Index()
{
    InputModel model = new InputModel();

    return View(model);
}

[HttpPost]
public ActionResult Index(InputModel model)
{
    model.Step++;
    return View(model);
}

Also my model.Step Property is always 0 (or 1, after 1 is added in the HttpPost), which is of course because my model won't be passed a second time.

Any suggestions?

tereško
  • 58,060
  • 25
  • 98
  • 150
Kevin Suppan
  • 232
  • 2
  • 18

1 Answers1

4

might be worth looking over previous blog examples on this topic (as well as a few here on SO):

in a nutshell, I would do something like the following, irrespective as to whether jQuery/javascript is allowed or not:

  • Create a model that contains ALL fields required by the wizard.
  • Create a NEW controller (for the wizard steps)
  • Create a single Action per wizard page (again, all actions should use the same wizard model)
  • Create a SAVE action at the end of the process that saves everything to the database.

Also, in your example above, you don't include the 'step' on the view inside the form tags. THis will be required on postback. You should add something like:

@Html.HiddenFor(model => model.Step)

your milage may vary - good luck

Community
  • 1
  • 1
jim tollan
  • 22,305
  • 4
  • 49
  • 63