【问题标题】:ASP.NET MVC 2 Pure jquery client side validationASP.NET MVC 2 纯 jquery 客户端验证
【发布时间】:2010-10-24 06:41:55
【问题描述】:

如何仅使用 jquery 连接我的验证?我不想使用 Microsoft ajax。我在this blog 上看到了我想要的,但似乎MicrosoftMvcJqueryValidator.js 文件已被弃用或取消。

现在有官方方法可以做到这一点吗?可能使用 asp.net mvc 3。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 validation asp.net-mvc-3


    【解决方案1】:

    ASP.NET MVC 3.0 Beta 1 模板中已包含实现此目标所需的一切。

    型号:

    public class MyViewModel
    {
        [Required]
        public string Value { get; set; }
    }
    

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    查看:

    <script type="text/javascript" src="<%: Url.Content("~/scripts/jquery-1.4.1.js") %>"></script>
    <script type="text/javascript" src="<%: Url.Content("~/scripts/jquery.validate.js") %>"></script>
    <script type="text/javascript" src="<%: Url.Content("~/scripts/jquery.validate.unobtrusive.js") %>"></script>
    
    <% Html.EnableClientValidation(); %>
    <% using (Html.BeginForm()) { %>
        <%: Html.TextBoxFor(x => x.Value) %>
        <%: Html.ValidationMessageFor(x => x.Value) %>
        <input type="submit" value="OK" />
    <% } %>
    

    如果您想用 ASP.NET MVC 2.0 做同样的事情,您需要下载 ASP.NET MVC Futures 的源代码并从包中提取 MicrosoftMvcJQueryValidation.js 以包含在您的站点中。

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-14
        • 2014-07-21
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多