Bind Prefix not worked in Remote validation Action

2019-06-03 16:14发布

问题:

I ask a similar question here. This is My Model:

[DisplayName("National Code")]
[Required(ErrorMessage = "Required")]
[RegularExpression(CustomRegex.SSNRX, ErrorMessage = CustomRegex.SSNErMsg)]
[Remote("DBValidateSSN", "CustomerProfile", "Members", ErrorMessage = "Repeated.")]
public string SSN { get; set; }

So I use a Generic Class as a Model And My View is like:

@Html.EditorFor(model => model.MainModel.SSN)
@Html.ValidationMessageFor(model => model.MainModel.SSN)

And Validate Action:

public JsonResult DBValidateSSN([Bind(Prefix = "MainModel")] string SSN) {

  // ....
  return Json(result, JsonRequestBehavior.AllowGet);
}

But the SSN parameter in action is always null, where is my fault? what is the problem I also check Ajax Request Params in FireBug and name used is MainModel.SSN, what is your suggestion?

回答1:

Try like this:

public ActionResult DBValidateSSN([Bind(Prefix = "MainModel.SSN")] string SSN)
{
    // ...
    return Json(result, JsonRequestBehavior.AllowGet);
}