MVC3客户端验证 - 我缺少什么?

时间:2011-11-09 14:03:34

标签: asp.net-mvc-3 client-side-validation

我已经用Google搜索,查看了博客,在这里检查了多个答案...当我启用"客户端验证"在我的MVC3应用程序中,验证只发生在服务器上,而不是客户端 - 我的表单发布了所需的文本输入,没有值,只是在服务器上退回。

完全披露:到目前为止,我已经完成了3个MVC项目中的任何一个,从未得到过这一点。我确信我在这里遗漏了一些基本的东西,因为我发现绝大多数的例子和操作方法都带有#34;这篇文章指的是beta版本"免责声明...

这通常是我感到沮丧并撕掉所有内容的地方;客户端必须是间接的和神奇的,因为我们害怕让开发人员编写javascript"垃圾,直接使用jquery.validation,但我想我会尝试在这里发帖,看看我是否遗漏了一些基本的东西......就像我通常看起来那样;)

此型号:

public class RegisterModel
{
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string Email { get; set; }
    [Required]
    public string Password { get; set; }
    [Required]
    public string ConfirmPassword { get; set; }
}

此视图(样式需要" textfield"类):

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <div class="form-item-right">
            @Html.ValidationMessageFor(model => model.FirstName, "First Name is required.")
            @Html.TextBoxFor(model => model.FirstName, new { @class = "textfield" })
            @Html.LabelFor(model => model.FirstName, "First Name")


        </div>

        <div class="form-item-right">
            @Html.ValidationMessageFor(model => model.LastName)
            @Html.TextBoxFor(model => model.LastName, new { @class = "textfield" })
            @Html.LabelFor(model => model.LastName, "Last Name")
        </div>

        <div class="form-item-right">
            @Html.ValidationMessageFor(model => model.Email)
            @Html.TextBoxFor(model => model.Email, new { @class = "textfield" })
            @Html.LabelFor(model => model.Email, "Email Address")

        </div>

        <div class="form-item-right">
            @Html.ValidationMessageFor(model => model.Password)
            @Html.PasswordFor(model => model.Password, new { @class = "textfield" })
            @Html.LabelFor(model => model.Password, "Password")

        </div>

        <div class="form-item-right">
            @Html.ValidationMessageFor(model => model.ConfirmPassword)
            @Html.PasswordFor(model => model.ConfirmPassword, new { @class = "textfield" })
            @Html.LabelFor(model => model.ConfirmPassword, "Confirm")

        </div>

        <div class="form-item-right">
            <input type="submit" value="Sign Up!" class="buttonSignUp"/>
        </div>



}

这会产生如下标记:

            <span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="false">First Name is required.</span>
            <input class="textfield" data-val="true" data-val-required="The FirstName field is required." id="FirstName" name="FirstName" value="" type="text">
            <label for="FirstName">First Name</label>

这些脚本通过布局视图包含:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js") "type="text/javascript"></script>

这是在web.config中:

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

完整性的控制器代码:

    public ActionResult Candidate()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Candidate(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            //complete the registration

            return View("Confirm", new ConfirmationModel { Email = model.Email, FirstName = model.FirstName });   
        }
        else
        {
            return View(model);
        }
    }

所有结果都在视图中没有在客户端进行验证。表格帖子根本没有输入任何字段。验证仅在服务器上进行(FWIW正常工作)。

出了什么问题?

编辑以包含视图中的结果标记。

1 个答案:

答案 0 :(得分:0)

我不知道你做错了什么,但这是你可以遵循的简单步骤:

  1. 使用默认模板
  2. 创建新的ASP.NET MVC 3项目
  3. 定义以下模型:

    public class MyViewModel
    {
        [Required]
        public string Foo { get; set; }
    }
    
  4. HomeController中:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    
  5. 索引视图:

    @model MyViewModel
    
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    
    @using (Html.BeginForm())
    {
        @Html.LabelFor(x => x.Foo)
        @Html.EditorFor(x => x.Foo)
        @Html.ValidationMessageFor(x => x.Foo)
        <button type="submit">OK</button>
    }
    
  6. 运行项目=&gt;客户端和服务器端验证按预期工作