3

I am trying to use the RemoteAttribute to validate a data element serverside using JSON. My data field is:

[Display(Name = "My Number")]
    [Required]
    [Remote("IsValidMyNumber","Home",ErrorMessage="Bummer")]
    public string MyNumber { get; set; }

My controller is:

  public JsonResult IsValidMyNumber(string MyNumber)
    {
        var test = services.ValidateMyNumber(MyNumber);
        return Json(test,JsonRequestBehavior.AllowGet);
    }

My view is:

    <div class="editor-field">
    @Html.EditorFor(model => model.CheckInformation.MyNumber) 
    @Html.ValidationMessageFor(model => model.CheckInformation.MyNumber)
</div>

The HTML generated is:

 <input class="text-box single-line" data-val="true" data-val-remote="Bummer" 
data-val-remote-additionalfields="*.MyNumber" data-val-remote-url="/Home/IsValidMyNumber" 
data-val-required="The Number field is required." id="CheckInformation_MyNumber"
 name="CheckInformation.MyNumber" type="text" value="" />

When I debug and step inside my controller the "MyNumber" parameter is null even though I have text in the textbox that this represents.

I know that the name has to be the same in the textbox as in the parameter and I have validated that.

Any ideas?

John S
  • 7,909
  • 21
  • 77
  • 145
  • Please post how is the generated html looks like for the `MyNumber` textbox! – nemesv Aug 07 '13 at 20:20
  • Just added the HTML code. – John S Aug 07 '13 at 20:27
  • Which version of asp.net mvc are you using? You are probably experiencing the same bug explained here: http://stackoverflow.com/a/8376646/872395 . You can either update your `jquery.validate.unobtrusive.js` from nuget or you apply the fix from the linked question. – nemesv Aug 07 '13 at 20:34
  • 1
    From what I can see you have CheckInformation.MyNumber, is CheckInformation your model? I think @nemesv was looking for how your view looks like? I think more detailed code example might be beneficial. – CrnaStena Aug 07 '13 at 20:35
  • I added the view code – John S Aug 07 '13 at 20:53

2 Answers2

6

Seems like the generated name of your input field is:

name="CheckInformation.MyNumber"

That's probably because your view model is a parent model of what you have shown in your question and you used something along the lines of:

@Html.TextBoxFor(x => x.CheckInformation.MyNumber)

So make sure you have specified this prefix or the default model binder will never be able to rehydrate the values:

public ActionResult IsValidMyNumber([Bind(Prefix = "CheckInformation")] string myNumber)
{
    var test = services.ValidateMyNumber(myNumber);
    return Json(test, JsonRequestBehavior.AllowGet);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

You can also receive the value of myNumber to pass your view model in the action parameter, Like. It works for me.

public ActionResult IsValidMyNumber(YourViewModel vm)
{
    var test = services.ValidateMyNumber(vm.myNumber);
    return Json(test, JsonRequestBehavior.AllowGet);
}
Safyan Yaqoob
  • 444
  • 3
  • 11