views:

24

answers:

2

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.

A: 

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

Mark Redman
+1  A: 

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.

Darin Dimitrov