我正在将现有站点从WebForms迁移到MVC 5。
我有一个有客户端验证的Contact Us页面,用于检查字段是否已完成等等。然而,我使用的是具有服务器端验证功能的BotDetect验证码,它可以检查验证码是否正确完成。
如果我输入正确的CAPTCHA,所有的工作如预期--一封电子邮件被发送,所有的表单字段都在电子邮件中被表示。
如果输入错误的CAPTCHA,应该将我返回到表单中,将显示无效CAPTCHA的错误消息,但所有其他字段仍应填入。
然而,这种情况并没有发生。表单字段丢失。我猜这是因为模型正在再生。我还没有弄明白如何让我的ActionResult放弃执行任何操作,并将表单内容保留在适当的位置。
我的看法是:
@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")
}控制器的一部分(删除了对无关操作的调用):
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());
}
}
}发布于 2015-10-23 21:03:29
返回到无效情况下的视图时,将模型传回以重新显示它(即当!ModelState.IsValid执行此操作时.)
return View(contactModel); // this is the one that was submittedhttps://stackoverflow.com/questions/33311285
复制相似问题