2010-10-24 77 views
0

我怎麼去關於我的驗證與jquery連接?我不想使用微軟的Ajax。我在this blog上看到了我想要的,但似乎文件MicrosoftMvcJqueryValidator.js已被棄用或取消。ASP.NET MVC 2純jQuery的客戶端驗證

現在有沒有正式的方法來做到這一點?可能使用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以包含在您的網站中。