1

I created a very simple test project to illustrate the problem.

My model class

public class HomeModel
{
    [Required(ErrorMessage="Missing property1.")]
    public string Property1
    {
        get;
        set;
    }

    [Remote("ValidateProperty2", "Home", HttpMethod="Get",  AdditionalFields = "Property3", ErrorMessage="Property2 wrong!")]
    public string Property2
    {
        get;
        set;
    }

    public string Property3
    {
        get;
        set;
    }
}

My controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
        HomeModel model = new HomeModel();
        return View(model);
    }

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

    public ActionResult ValidateProperty2(string property2, string property3)
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }
}

And my view

@model RemoteValidationTest.Models.HomeModel

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<title>Index</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>
<body>
@using (Ajax.BeginForm(new AjaxOptions
{
    HttpMethod = "POST"
}))
{
    @Html.TextBoxFor(x => x.Property1) @Html.ValidationMessageFor(x => x.Property1)<br />
    @Html.TextBoxFor(x => x.Property2) @Html.ValidationMessageFor(x => x.Property2)<br />
    @Html.TextBoxFor(x => x.Property3) @Html.ValidationMessageFor(x => x.Property3)<br />

    <input type="submit" />
}
</body>
</html>

And my web.config

<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/> 

Nothing really fancy here. I havea model class with 3 properties. The first one is set be required and second one is remote validation, I believed I have create the action method properly. I set break piont to ValidateProperty2 function, it never gets called.

I also used FireBug, the same thing client side does not even try to call the server side.

What is wrong with the code here?

Edit 1: I think I get something, the remote validation will only fires when the control (e.g text box) has value in side. The empty control will never trigger the validation. In my case I actually try to implmenet a more complicated logic, I need the validation to fire even when the control text is empty (to check other property's value). Is it even possible?

hardywang
  • 4,864
  • 11
  • 65
  • 101
  • Did you confirm this works without the AdditionalFields attribute? Take a look at this: http://stackoverflow.com/questions/4752877/remote-validation-in-asp-net-mvc-3-how-to-use-additionalfields-in-action-method – Mihalis Bagos Dec 01 '11 at 20:11
  • @Mihalis Bagos, it does not fire without AdditionalFields. – hardywang Dec 01 '11 at 20:23
  • Since the code looks ok then, consider updating the js libraries through NuGet and try again. Also, you can find a working example here: http://msdn.microsoft.com/en-us/library/gg508808%28v=vs.98%29.aspx – Mihalis Bagos Dec 01 '11 at 20:25
  • I think I get something, the remote validation will only fires when the control (e.g text box) has value in side. The empty control will never trigger the validation. – hardywang Dec 01 '11 at 21:14
  • ah now I see, posting as an answer – Mihalis Bagos Dec 01 '11 at 23:18

2 Answers2

0

I have a working version here for mvc3: http://completedevelopment.blogspot.com/2011/08/remote-ajax-validation-in-mvc3.html

d/l it and you can compare files.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • I think I broke your code, the downloaded project just works fine. I added [Remote("AddressValidate", "Demo", ErrorMessage = "Address is invalid.")] public string Address { get; set; } to your Person class, removed RegularExpression and Required from your EmailAddress property. Now my Address is not validated at all. – hardywang Dec 01 '11 at 21:06
0

The problem is that the validation don't fire automatically when you press the send button for the first time. Here is how you initiate the validation upon page load so that it fires every time it is needed on submit:

"Required" validation attribute not working in asp.net mvc 3 while others work

Community
  • 1
  • 1
Mihalis Bagos
  • 2,500
  • 1
  • 22
  • 32