首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC表单在服务器端验证中丢失值

MVC表单在服务器端验证中丢失值
EN

Stack Overflow用户
提问于 2015-10-23 20:58:47
回答 1查看 1.1K关注 0票数 0

我正在将现有站点从WebForms迁移到MVC 5。

我有一个有客户端验证的Contact Us页面,用于检查字段是否已完成等等。然而,我使用的是具有服务器端验证功能的BotDetect验证码,它可以检查验证码是否正确完成。

如果我输入正确的CAPTCHA,所有的工作如预期--一封电子邮件被发送,所有的表单字段都在电子邮件中被表示。

如果输入错误的CAPTCHA,应该将我返回到表单中,将显示无效CAPTCHA的错误消息,但所有其他字段仍应填入。

然而,这种情况并没有发生。表单字段丢失。我猜这是因为模型正在再生。我还没有弄明白如何让我的ActionResult放弃执行任何操作,并将表单内容保留在适当的位置。

我的看法是:

代码语言:javascript
复制
@using BotDetect.Web.UI.Mvc;
@model Framework.Models.FrameworkModel

@section Styles {
    <link href="@BotDetect.Web.CaptchaUrls.Absolute.LayoutStyleSheetUrl" rel="stylesheet" type="text/css" />
}

<h1>@Html.Raw(Model.Page.Heading)</h1>
<div class="main-container">
    @if (string.IsNullOrWhiteSpace(ViewBag.Status))
    {
        @Html.Raw(Model.Page.Body)
        <br />
        <fieldset id="ContactFields" class="contact-fields">
            <ol>
                <li>
                    <label id="FullNameLabel" labelfor="FullName">@Resources.Contact.FullNameLabel</label>
                    @Html.TextBoxFor(model => model.Contact.FullName, new { @id = "FullName", @Name = "FullName", @class = "standard-textbox", @autocompletetype = "DisplayName", @data_validation_required = Resources.Contact.FullNameValidationErrorMessage})
                </li>
                <li>
                    <label id="EmailLabel" labelfor="Email">@Resources.Contact.EmailLabel</label>
                    @Html.TextBoxFor(model => model.Contact.Email, new { @id = "Email", @Name = "Email", @class = "standard-textbox", @autocompletetype = "Email", @data_validation_required = Resources.Contact.EmailValidationErrorMessage, @data_validation_format = Resources.Contact.EmailValidationErrorMessageFormat })
                </li>
                <li>
                    <label id="SubjectLabel" labelfor="Subject">@Resources.Contact.SubjectLabel</label>
                    @Html.TextBoxFor(model => model.Contact.Subject, new { @id = "Subject", @Name = "Subject", @class = "standard-textbox", @data_validation_required = Resources.Contact.SubjectValidationErrorMessage })
                </li>
                <li>
                    <label id="MessageLabel" labelfor="Message">@Resources.Contact.MessageLabel</label>
                    @Html.TextAreaFor(model => model.Contact.Message, new { @id = "Message", @Name = "Message", @class = "multiline-textbox", @rows = 5, @data_validation_required = Resources.Contact.MessageValidationErrorMessage })
                </li>
                <li>
                    <div class="captcha-control">
                        <br />
                        @{ MvcCaptcha captcha = Model.CaptchaHelper.GenerateCaptcha(); }
                        @Html.Captcha(captcha)
                        <br />
                        @Html.TextBox("CaptchaCode", "", new { @id = "CaptchaCode", @Name = "CaptchaCode", @class = "captcha-code short-textbox", @data_validation_required = Resources.Captcha.CaptchaValidationErrorMessage })
                        @Html.ValidationMessage("CaptchaCode")
                    </div>
                    <br />
                    <button id="SendButton" type="submit" class="send-button">@Resources.Contact.SendButton</button>
            </ol>
        </fieldset>
    }
    else
    {
        <span class="alert">@Html.Raw(ViewBag.Status)</span>
    }
</div>

@section Scripts {
    @Scripts.RenderFormat("<script type=\"text/javascript\" src=\"{0}\" defer=\"defer\"></script>", "~/bundles/scripts/contact")
}

控制器的一部分(删除了对无关操作的调用):

代码语言:javascript
复制
using Framework.App_Code.ViewRendering;
using Framework.Models;
using System.Web.Mvc;
using BotDetect.Web.UI.Mvc;

namespace Framework.Controllers
{
    public class PagesController : AsyncController
    {
        [HttpGet]
        [ActionName("Contact")]
        public ActionResult Contact()
        {
            Contact viewRendering = new Contact();
            return View(viewRendering.GenerateModel());
        }

        [HttpPost]
        [ActionName("Contact")]
        [AllowAnonymous]
        [CaptchaValidation("CaptchaCode", "Captcha")]
        public ActionResult ContactPost(ContactModel contactModel, bool captchaValid)
        {
            Contact viewRendering = new Contact();
            if (ModelState.IsValid)
            {
                contactModel.IPAddress = Request.ServerVariables["REMOTE_ADDR"].Trim();
                ViewBag.Status = viewRendering.SendMail(contactModel);
            }
            MvcCaptcha.ResetCaptcha("Captcha");
            return View(viewRendering.GenerateModel());
        }

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-23 21:03:29

返回到无效情况下的视图时,将模型传回以重新显示它(即当!ModelState.IsValid执行此操作时.)

代码语言:javascript
复制
return View(contactModel); // this is the one that was submitted
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33311285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档