asp.net-mvccredit-cardasp.net-mvc-2-validation

Persist non-model info on an MVC form


I have an MVC form that contains some fields that are not related to my model. Those fields contain user-supplied Credit Card information. Once the user fills-out the form and clicks Continue, I perform some credit-card validation in my controller. If the credit-card transaction is successful, I pay attention to the other Model-related fields, and I take the user to a Finish/confirmation page. That much works great.

However, if the credit-card transaction is unsuccessful, I basically want to keep the user on the same page with all fields still filled-in, even these credit-card fields that have nothing to do w/ my Model. This has proved to be difficult because all of the fields on the form seem to get wiped-out.

Any help is appreciated. Here are a couple of my form controls:

<tr>
    <td>ccFieldA:</td>
    <td>
        <%= Html.TextBox("ccFieldA") %>
    </td>
    <td>
        <label id="ccFieldAError" runat="server"></label>
        <%= Html.Hidden("hiddenFieldA") %>
    </td>
</tr>
<tr>
    <td>ccFieldB:</td>
    <td><%= Html.TextBox("ccFieldB") %></td>
    <td>
        <label id="ccFieldBError" runat="server"></label>
        <%= Html.Hidden("hiddenFieldB") %>
    </td>
</tr>

Then, in my controller, I do something like this on form-submit:

if (CreditCardPassesValidation()) {
    return RedirectToAction("NextPage", new { id = myID });
}
else {
    return View(ThisSamePage);
}

Solution

  • Basically you need to create a view model for your form. And in the action if the transaction is not successfull you can pass that view model again back to the view . And in the view you need to user HTML helpers like this,

    <%= Html.TextBoxFor(model=>model.ccFieldA) %>
    

    and in the action you can return the view model like this,

    public ActionResult YourAction(YourViewModel model)
        {
           if (CreditCardPassesValidation()) {
                return RedirectToAction("NextPage", new { id = myID });
           }
           else {
                 return View(ThisSamePage,model);
           }
        }