代码之家  ›  专栏  ›  技术社区  ›  Shawn Mclean

ASP.NET MVC 2纯jquery客户端验证

  •  0
  • Shawn Mclean  · 技术社区  · 15 年前

    如何将验证仅与jquery挂钩?我不想使用Microsoft Ajax。我看到了 this blog 正是我想要的,但文件似乎 MicrosoftMvcJqueryValidator.js 已弃用或取消。

    现在有正式的方法吗?可能使用ASP.NET MVC 3。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Darin Dimitrov    15 年前

    实现这一点所需的一切都已包含在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
  •  0
  •   Mark Redman    15 年前