【问题标题】:CaptchaMvc using Ajax.beginform asp.net mvc4CaptchaMvc 使用 Ajax.beginform asp.net mvc4
【发布时间】:2015-05-01 07:25:28
【问题描述】:

我在我的 asp.net mvc4 应用程序中使用 CaptchaMvc.Mvc4 1.5.0。 这是我的看法:

@using CaptchaMvc.HtmlHelpers;
@using CaptchaMvc;
@model Web.Models.Message
@using (Ajax.BeginForm("Contact", "Home", new AjaxOptions
{
    HttpMethod = "post",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "Sent"
}
{
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
            <fieldset>                       
            @Html.LabelFor(model => model.Name)
            <div>
            @Html.TextBoxFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
            </div>  
            @Html.LabelFor(model => model.Email)
            <div>
            @Html.TextBoxFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
            </div>                              
            @Html.LabelFor(model => model.Message1)
            <div>
            @Html.TextBoxFor(model => model.Message1)
            @Html.ValidationMessageFor(model => model.Message1)
            </div>

            @Html.MathCaptcha()

            <input type="submit" value="Create" />
            </fieldset>

            <div id="Sent">
            </div>
  }

这是我的行动方法:

[HttpPost]
public ActionResult Contact(Message message)
{
   if(this.IsCaptchaValid("error"))           
   if (ModelState.IsValid )
   {
       db.Messages.Add(message);
       db.SaveChanges();
       return RedirectToAction("SuccessfullySent");
    }

    ModelState.AddModelError("", "there is some error");
    return View(message);
}

验证码输入有效时可以正常工作。 问题是当验证码无效时,整个视图都呈现在 id="Sent" 的 div 中。所以我在一页中呈现了两个消息视图。 如何防止它在 id="Sent" 的 div 中再次呈现整个视图?

【问题讨论】:

    标签: asp.net ajax asp.net-mvc asp.net-mvc-4 captcha


    【解决方案1】:

    您返回的 View 与您最初用于呈现页面的相同。

    这意味着更新的 div 正在被整个页面替换。

    您可以按如下方式返回消息:

    return Content("there is some error");
    

    这会将消息输出到已发送的 div。

    如果PartialView 包含更多 Html 并按如下方式传递消息,您也可以返回一个 PartialView

    return PartialView("_errorPartial", "there is some error"); 
    

    【讨论】:

    • tnx 配合它工作。但现在还有另一个问题。返回后返回内容(“有一些错误”);页面处理完毕,我无法重新提交消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2012-12-16
    • 2011-12-23
    相关资源
    最近更新 更多