DataAnnotations驾驶客户端验证问题 [英] DataAnnotations driving client side validation issue

查看:106
本文介绍了DataAnnotations驾驶客户端验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得什么,我认为是用一个模型DataAnnotations来驱动客户端验证,以及一个简单的例子。

I am trying to get what I think is a simple example of using DataAnnotations on a model to drive the client side validation as well.

下面是我的模型......

Here is my model...

 public class Person
 {
  [Required(ErrorMessage = "First Name Required")]
  public string FirstName { get; set; }
  [Required(ErrorMessage = "Last Name Required")]
  public string LastName { get; set; }
 }

下面是我的控制器...

Here is my controller...

 public class FriendsController : Controller
 {
  public ActionResult Create()
  {
   Person newFriend = new Person();
   return View(newFriend);
  }

  [HttpPost]
  public ActionResult Create(Person friendToCreate)
  {
   if (ModelState.IsValid)
   {
    // todo -- do something here
    return Redirect("/");
   }

   // Invalid - redisplay form with errors
   return View(friendToCreate);
  }
 }

这是我的观点...

and here is my view...

@model MvcApplication4.Models.Person
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script>
</head>
<body>
    <h2>
        Create</h2>
    @{Html.EnableClientValidation();}
    @using (Html.BeginForm())
    { 
        <fieldset>
            <p>
                @Html.LabelFor(m => m.FirstName)
                @Html.TextBoxFor(m => m.FirstName)
                @Html.ValidationMessageFor(m => m.FirstName)
            </p>
            <p>
                @Html.LabelFor(m => m.LastName)
                @Html.TextBoxFor(m => m.LastName)
                @Html.ValidationMessageFor(m => m.LastName)
            </p>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
</body>
</html>

服务器端验证工作得很好,并出现验证错误消息预期。但是,我没有得到的客户端验证工作。有什么明显的,我很想念,使客户端验证出现?

The server side validation works just fine and the validation error messages appear as expected. However, I am not getting the client side validation to work. Is there something obvious that I am missing to make the client side validation appear?

推荐答案

你在你的web.config文件中启用客户端验证?

Did you enabled Client side Validation on your web.config file?

您可以直接做在web.config文件添加几个标志的appSetting栏目

You can do it directly on the web.config file adding a couple of flags inside the appSetting section

<configuration>
    <appSettings>
        <add key="ClientValidationEnabled" value="true"/>
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>
</configuration>

,也可以使用纯C#code做

or you can do it using pure c# code

HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

我建议你阅读布拉德·威尔逊的文章不显眼的客户端验证的ASP .NET MVC 3

这篇关于DataAnnotations驾驶客户端验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆