2

I am having a problem with remote validation.

I have a viewmodel with a property on which I have added a Remote validator but when I run the form and enter a string in the text box the value passed to the controller is null.

The property in the viewmodel looks like this:

[Required(ErrorMessage = "Enter the host's name")]
[Remote("ValidateHostFullName", "BoardroomBooking", ErrorMessage = "Enter a different name")]
[DisplayName("Host's Name")]
public string HostFullName { get; set; }

The code for the validator in the Controller looks like this:

public ActionResult ValidateHostFullName([Bind(Prefix="BookingReceptionViewModel")]string HostFullName)
{
    if (!HostFullName.Equals("John Smith"))
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }

    return Json("{0} is not allowed", JsonRequestBehavior.AllowGet);
}

The value of the string for HostFullName shows as null no matter what is typed in the box. I have tried it with and without the Bind Prefix and that makes no difference.

I've tried this on a model and it works, it only seems to have an issue when I use a viewmodel.

Thanks

Mark

6
  • 1
    What is the name of the corresponding input field in the generated HTML? What value exactly is POSTed in the AJAX request when you inspect with FireBug? Commented Oct 19, 2011 at 16:03
  • 1
    @DarinDimitrov The name of the field is generated as HostFullName. The request shows as "GET /BoardroomBooking/ValidateHostFullName?callback=jQuery15104607706305105239_1319095613374&HostFullName=John+Smith&_=1319095674721" in FireBug. Commented Oct 20, 2011 at 7:47
  • 1
    Where does the callback parameter come from? Also the query string parameter is called HostFullName so you don't need the Bind attribute on your action. But I think that you have other problems with this request as well. It looks like a JSONP request which is not what it should be. Commented Oct 20, 2011 at 12:14
  • I had tried it without the Bind attribute and got the same result so I had put that in to see if it made any difference. I'm not certain where that callback parameter comes from. I wasn't expecting to see that - I had looked at other issues here and seen how the request should be formed and none of them had anything like that. I'll do some digging and see what the cause of that is. Commented Oct 20, 2011 at 13:59
  • 1
    @DarinDimitrov Thanks a lot for your help on this - it pointed me in the right direction. I've changed the ValidateHostFullName actionresult to accept HttpPost and removed the Bind attribute and it now returns properly. I still have no idea where the JSONP request is coming from in the original code. I've disabled all 3rd party scripts and it just keeps appearing. Commented Oct 21, 2011 at 9:31

2 Answers 2

4

I was having the same issue. The parameter coming into ValidateHostFullName() must be the same as the input name.

Sign up to request clarification or add additional context in comments.

1 Comment

I agree with answer posted. I experimented it. The of the parameter must match the input name, case insensitive though (my input name is mix cases, but the parameter can be all lowercase). That is very bad limitation.
0

I had the same problem. The rendered html control was NOT prefixed by class name but in remote validation code, I had binded by prefixing the classname.propertyname. Removing this binding solved my problem. Or else by prefixing only property name also works fine for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.