通过 DataAnnotations 进行 ASP.Net MVC 验证

发布于 2024-09-16 23:58:54 字数 228 浏览 5 评论 0原文

我关注了 scottgu 的博客 这里并尝试进行数据验证。成功了。然而,我看到的是,如果我的字段是必填字段,那么一旦我从文本框中失去焦点,我就会收到错误。我希望仅在单击“提交”时才进行验证。

I followed scottgu's blog here and tried to do data validation. Succeeded. However what I see is that if my field is a required field, i get an error as soon as i loose focus from my textbox. I want validation to happen only when I click submit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭了丶谁疼 2024-09-23 23:58:54

该文章是关于使用客户端验证,因此您可以在客户端验证表单。因此,当您失去焦点时,jquery 会验证表单!

一种方法是使用服务器端验证。在这种情况下,您将刷新页面。

更新:

这是示例代码。
模型:

public class GuestForm
    {
        [Required(ErrorMessage="Please enter your name")]
        public string Name { get; set; }

        [Required(ErrorMessage="Please enter your phone number")]
        public string Phone { get; set; }

        [Required(ErrorMessage="Please enter your email address")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Please enter your choice")]
        public bool? YesNo { get; set; }
    }

表单:

<div>
<% using(Html.BeginForm()) { %>
    <%= Html.ValidationSummary() %>
    Name: <%= Html.TextBoxFor(x => x.Name) %><br/>
    Email: <%= Html.TextBoxFor(x => x.Email) %><br/>
    Phone: <%= Html.TextBoxFor(x => x.Phone) %><br/>
    Will you attend?
    <%= Html.DropDownListFor(x => x.YesNo, new[] {
      new SelectListItem { Text = "Yes",Value = bool.TrueString },
      new SelectListItem { Text = "No",Value = bool.FalseString }
      }, "Choose...") %><br/>
   <input type="submit" value="Register!" />
<% } %>
</div>

控制器:

        [HttpGet]
        public ViewResult RegisterForm()
        {
            return View();
        }

        [HttpPost]
        public ViewResult RegisterForm(GuestForm form)
        {
            if (ModelState.IsValid)
                return View("Thanks", form);
            else
                return View();
        }

当用户单击“提交”时,此代码将看到您的表单在服务器端得到验证。
它将以列表的形式将错误消息显示在表单顶部。

我希望这对你有帮助。

that article is about using client side validation hence you get your form validated on client side. due to this, the form is validated by jquery when you loose the focus!

one way would be to use the server side validation.. i that case you will have a page refresh.

Update:

Here is the sample code.
Model:

public class GuestForm
    {
        [Required(ErrorMessage="Please enter your name")]
        public string Name { get; set; }

        [Required(ErrorMessage="Please enter your phone number")]
        public string Phone { get; set; }

        [Required(ErrorMessage="Please enter your email address")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Please enter your choice")]
        public bool? YesNo { get; set; }
    }

Form:

<div>
<% using(Html.BeginForm()) { %>
    <%= Html.ValidationSummary() %>
    Name: <%= Html.TextBoxFor(x => x.Name) %><br/>
    Email: <%= Html.TextBoxFor(x => x.Email) %><br/>
    Phone: <%= Html.TextBoxFor(x => x.Phone) %><br/>
    Will you attend?
    <%= Html.DropDownListFor(x => x.YesNo, new[] {
      new SelectListItem { Text = "Yes",Value = bool.TrueString },
      new SelectListItem { Text = "No",Value = bool.FalseString }
      }, "Choose...") %><br/>
   <input type="submit" value="Register!" />
<% } %>
</div>

Controller:

        [HttpGet]
        public ViewResult RegisterForm()
        {
            return View();
        }

        [HttpPost]
        public ViewResult RegisterForm(GuestForm form)
        {
            if (ModelState.IsValid)
                return View("Thanks", form);
            else
                return View();
        }

this code will see that your form is validated on server side when user click submit.
it will display the error message on top of the form in the form of list.

i hope this helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文