开发者

ASP.NET MVC 2 Pure jquery client side validation

How do I go about hooking up my validation with jquery only? I do not want to use Microsoft ajax. I saw on this blog exactly what I want, but it seems that the file MicrosoftMvcJqueryValidator.js is deprecated开发者_如何学运维 or canceled.

Is there an official way to do this now? Possibly using asp.net mvc 3.


Everything you need to achieve this is already included with the ASP.NET MVC 3.0 Beta 1 template.

Model:

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

Controller:

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

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

View:

<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" />
<% } %>

If you want to do the same thing with ASP.NET MVC 2.0 you will need to download the source code of ASP.NET MVC Futures and extract MicrosoftMvcJQueryValidation.js from the package to include in your site.


You can use this plugin: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜