在 MVC 表单上保留非模型信息

发布于 2024-12-22 07:04:06 字数 1038 浏览 2 评论 0原文

我有一个 MVC 表单,其中包含一些与我的模型无关的字段。这些字段包含用户提供的信用卡信息。用户填写表单并单击“继续”后,我会在控制器中执行一些信用卡验证。如果信用卡交易成功,我会关注其他与模型相关的字段,并将用户带到完成/确认页面。就这么多,效果很好。

但是,如果信用卡交易不成功,我基本上希望让用户保持在同一页面上,所有字段仍然填写,即使这些信用卡字段与我的模型无关。事实证明这很困难,因为表单上的所有字段似乎都被删除了。

任何帮助表示赞赏。这是我的几个表单控件:

<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>

然后,在我的控制器中,我在表单提交上执行类似的操作:

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

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2024-12-29 07:04:06

基本上,您需要为表单创建一个视图模型。在操作中,如果事务不成功,您可以将该视图模型再次传递回视图。在视图中,您需要像这样使用 HTML 帮助程序,

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

并且在操作中,您可以像这样返回视图模型,

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

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);
       }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文