@Html.ValidationSummary() 在 Ajax.BeginForm 中不起作用

2024-04-15

使用有什么问题吗@Html.ValidationSummary()里面一个Ajax.BeginForm form?

我遇到以下情况,但无法验证必填字段。表单刚刚发布,也没有抛出任何错误。

这是视图:

@using (Ajax.BeginForm("Register", "Account", new AjaxOptions { HttpMethod = "POST", OnSuccess = "closeDialog('RegistroUsuario')" }))
{
   @Html.ValidationSummary() 
   <fieldset>
        <legend>Cadastro novo Usuário</legend>
       <table id="changePassword">
                <tr>
                    <td class="smallField">Username:</td>
                    <td>@Html.TextBoxFor(m => m.UserName)</td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td>@Html.PasswordFor(m => m.Password)</td>
                </tr>
                <tr>
                    <td>Repetir Senha:</td>
                    <td>@Html.PasswordFor(m => m.ConfirmPassword)</td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td>@Html.TextBoxFor(m => m.Email)</td>
                </tr>
                <tr>
                    <td>Pergunta Secreta:</td>
                    <td>@Html.TextBoxFor(m => m.SecretQuestion)</td>
                </tr>
                               <tr>
                    <td>Resposta:</td>
                    <td>@Html.TextBoxFor(m => m.SecretQuestionPassword)</td>
                </tr>
                <tr>
                    <td>Ativo:</td>
                    <td><input type="checkbox" name="status" id="status" value="Ativo"></td>
                </tr>    
            </table>           
    </fieldset>
    <input type="submit" value="Criar Usuário" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only button-link"/>
}

那是控制器:

//
    // POST: /Account/Register
    [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            MembershipProvider mp  = Membership.Provider;
            MembershipCreateStatus Status;                

            // Tenta registrar o usuário
            try
            {
                //Verifica se usuário deve estar ativo ou não no sistema
                if (String.IsNullOrEmpty(Request.Form["status"]))
                {
                    model.Active = false;
                }
                else
                {
                    model.Active = true;
                }

                //Cria o usuário
                MembershipUser newUser = mp.CreateUser(model.UserName, model.Password, model.Email, model.SecretQuestion, model.SecretQuestionPassword, model.Active, Guid.NewGuid(), out Status);

                if (newUser == null)
                {   
                    /**/
                }
                else
                {                       
                    return RedirectToAction("Index", "Home");   
                }

            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

和模型:

public class RegisterModel
{
    [Required]
    [Display(Name = "Usuário")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "A {0} deve ter no mínimo {2} caracteres.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Senha")]
    public string Password { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Repetir Senha")]
    [Compare("Password", ErrorMessage = "As senhas não coincidem")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "Pergunta Secreta")]
    public string SecretQuestion { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "A {0} deve ter no mínimo {2} caracteres.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Senha Pergunta Secreta")]
    public string SecretQuestionPassword { get; set; }

    [Required]
    [Display(Name = "Ativo")]
    public bool Active { get; set; }
}

我错过了什么吗?


发现问题了。

未设置 Ajax.Options 目标 ID。一旦我设置了验证 dom 元素,我就会收到消息。

多谢

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

@Html.ValidationSummary() 在 Ajax.BeginForm 中不起作用 的相关文章

  • 如何注销会话 MVC Razor Visual Studio

    我正在尝试从 MVC Razor 中的会话注销 这是我目前 MainController 中的内容 HttpPost public ActionResult Login Users user if ModelState IsValid if
  • MVC Ajax.BeginForm 替换奇怪的行为

    在部分视图中 我使用 MVC Ajax Beginform 如下所示 div using Ajax BeginForm Action Controller new AjaxOptions InsertionMode System Web M
  • ASP.NET MVC Ajax 使用 jquery 表单插件上传文件?

    我使用 Jquery Ajax 表单插件来上传文件 代码 作者视图模型 public class AuthorViewModel public int Id get set Required ErrorMessage 0 alan bo b
  • ASP.NET MVC 4 中的全局变量

    我目前正在构建一个 ASP Net MVC 4 SAAS 应用程序 C 并且一直致力于设计计划 我的意思是如果客户选择Plan A他们应该有权访问一些东西 如果他们选择的话Plan B他们可以接触其他人等等 我坚持的部分是与所有操作共享帐户
  • MVC 4 中的运行时动态捆绑和缩小

    我想知道是否有人可以帮助我使用 MVC 4 附带的新优化命名空间进行捆绑和缩小 我有一个多租户应用程序 我想在其中决定应根据每个用户的设置加载哪些 js 文件 一种方法是预先创建所有包并根据用户的设置更改resolvebundleurl的虚
  • 为什么连接没有在我的 iSeries/ASP.NET MVC 4 应用程序中重用?

    我们正在 Windows 2008 服务器场上运行 MVC 4 Web 应用程序 我们一直在尝试将服务器场升级到 Windows 2008 R2 64 位服务器 但在 iSeries 运行 V7R1 上遇到了连接池问题 我们经常调用 DB2
  • ASP.Net Web Api 未在 POST 上绑定模型

    我正在尝试将 JSON 数据发布到 Web Api 方法 但 JSON 数据未绑定到模型 这是我的模型 DataContract public class RegisterDataModel DataMember IsRequired tr
  • 验证仅适用于数组的第一项

    给定这个模型代码 Required Display Name Name public string Name get set 以下查看代码有效 Html LabelFor model gt model Name Html TextBoxFo
  • Windows Workflow Foundation 4 和 ASP.NET MVC

    我们正在评估 Windows Workflow Foundation 4 在基于 MVC 3 的 Web 应用程序中的使用 我们希望为不同的项目创建灵活的订单工作流程 有人知道有关此类应用程序的一般架构或实践实验室的详细信息吗 一些具体问题
  • ASP.NET MVC 中的异步控制器

    我检查了 MVC 4 0 中的 System Web Mvc AsyncController 它有注释 提供与 ASP NET MVC 3 的向后兼容性 这是否意味着 MVC 4 中有异步控制器的新实现 MVC 4 0 启用异步控制器以便将
  • ASP MVC4 - 通过视图模型传递列表以查看

    我有一个模型人物 其中包括出生日期等字段 我想将所有人的列表以及每个人的计算年龄传递给视图 因此 视图模型 public class vm PersonList public Person Person get set public int
  • 从 MVC 控制器返回 JSON 字符串

    我使用以下代码将对象发送 接收到我的 mvc 控制器 ajax url createOrUpdateTimeRecord data JSON stringify data type POST dataType json contentTyp
  • 我的 POST 编辑操作是否会覆盖 EF 中的所有字段?

    假设我有一个包含 7 个字段的数据库记录 我只想编辑字段 1 的内容 所以我点击了 GET EDIT 操作 它使用我的视图模型呈现强类型视图 然后我继续更新字段 1 但是 我的POST 操作包含所有字段的 映射 如下所示 实体框架也是如此
  • 字段“日期”必须是日期 MVC 4

    当我使用 jQuery 日期选择器时 我遇到了这个错误 jQuery datepicker datepicker defaultDate 7 autoSize true dateFormat dd mm yy Model DisplayNa
  • 具有 MVC4 风格捆绑的 Intellisense

    到目前为止我找不到问题或解决这个问题 我确信我错过了一些简单的事情 我有一个带有一堆缩小的 CSS 的样式包 并且我正在用里面的类来装饰 HTML 元素 一切都运转良好 然而 Intellisense 和 ReSharper 都因为 CSS
  • MVC4:以电子邮件作为参数的 url 路由

    我有这个 url 在我的 VS Web 服务器上运行得非常好 http localhost 4454 cms account edituser email protected cdn cgi l email protection works
  • 将我的自定义模型绑定到我的 POST 控制器

    我正在使用最近发布的 MVC 4 Beta 4 0 20126 16343 并且正在努力解决反序列化 模型绑定不适用于数组的已知问题 请参阅此处堆栈溢出 https stackoverflow com questions 9592838 d
  • 如何在 asp.net mvc 4 中使用 signalr v2 beta

    v2 之前 RouteTable Routes MapHubs 在 v2 中 MapHubs 不再存在 wiki 说添加一个 Startup 类和一个 Configuration 方法以及对 app MapHubs 的调用 namespac
  • 有条件地在 html.RadioButtonFor (MVC4/Razor) 中包含选中的属性

    当您在手动编码的 html 元素 例如单选按钮 中显式包含 checked 属性时 您可以使用 bool 来确定该属性是否存在于该元素上正如这里所看到的 http www davidhayden me blog conditional at
  • 当checked属性为false时,单选按钮默认被选中?

    我正在尝试生成单选按钮 有条件地检查检查的属性 如果该值存在于数据库中 则应选择它 否则检查的属性为 false 所以最初数据库中没有行 所有单选按钮的检查属性也为 false 但仍然在 UI 上选择 请参见下图 所以不知道这是默认行为还是

随机推荐