如何在MVC中回发时保留FormCollection值

时间:2018-09-20 13:44:59

标签: asp.net-mvc model-view-controller

在这里,我正在使用简单的应用程序,单击“发送”按钮后,我将执行Index httppost操作。但是由于某种原因,如果我的验证码不正确,那么我试图加载相同的视图。我正在传递表单集合,因此我的名字和姓氏不会消失。请提出我如何坚持我的价值观。

[HttpPost]
        public ActionResult Index(FormCollection dataColl)
        {
            ColComments datgrp = new ColComments();
            datgrp.fname = dataColl[0].ToString();
            datgrp.lname = dataColl[1].ToString();

            if (!this.IsCaptchaValid(""))
            {

                ViewBag.Classname = "alert alert-warning";
                ViewBag.ErrorMessage = "Incorrect captcha answer.";
            }
            else
            {
                ViewBag.ErrorMessage = "OKAY";
                return RedirectToAction("Landing", "Account");
            }

            return View(dataColl);
        }

索引视图。

@using CaptchaMvc.HtmlHelpers
@model CaptchaTestApp.Models.ColComments
@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>

</div>
<div>
    @using (Html.BeginForm())
    {
        <div> @Html.ValidationSummary(true)</div>
            <fieldset>
                <legend>ColComments</legend>

                <div class="editor-label">
                    @Html.LabelFor(model => model.fname)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.fname)
                    @Html.ValidationMessageFor(model => model.fname)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.lname)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.lname)
                    @Html.ValidationMessageFor(model => model.lname)
                </div>
            </fieldset>
                <table>
                    <tr>
                        <td colspan="3">
                            @Html.Captcha(8, "_captchaCnt")
                        </td>
                    </tr>
                </table>
                <p>

                    <input type="submit" value="Send" />
                </p>
    }
</div>

1 个答案:

答案 0 :(得分:0)

您需要返回带有更新值的HttpGet方法中发送的相同视图。您正在发送表单集合对象以进行查看。

[HttpPost]
        public ActionResult Index(FormCollection dataColl)
        {
            ColComments datgrp = new ColComments();
            datgrp.fname = dataColl[0].ToString();
            datgrp.lname = dataColl[1].ToString();

            if (!this.IsCaptchaValid(""))
            {

                ViewBag.Classname = "alert alert-warning";
                ViewBag.ErrorMessage = "Incorrect captcha answer.";
            }
            else
            {
                ViewBag.ErrorMessage = "OKAY";
                return RedirectToAction("Index", "Home");
            }

            return View(datgrp);
        }