現在、既存の「リモート」アトリビュートにアトリビュート・シミュレータを作成しています。クライアント側の検証は、javasciptが入力が有効であることを確認するアクションを呼び出すことで(これは私たちのデータベースでもあります)前進しています。問題はサーバー側の検証になるときです。私はそのアクションをどのように呼び出すことができますか? 「リモート側の」属性は「サーバ側の検証はしません」ので助けにはなりません検証アトリビュートから指定されたアクションを呼び出す
クライアント側のコードは正しく動作しているので、showは表示されません。
属性
[AttributeUsage(AttributeTargets.Property)]
public class AjaxValidation : Attribute, IModelValidator {
private readonly string _action;
private readonly string _area;
private readonly string _controller;
public AjaxValidation(string action, string controller = null, string area = null) {
_action = action;
_area = area;
_controller = controller;
}
public IEnumerable<ModelValidationResult> Validate(ModelValidationContext context) {
List<ModelValidationResult> result = new List<ModelValidationResult>();
//Need to call the action and check the result here
//Create the controller with reflection?
//Call the method with reflection?
if(false was returned) {
result.Add(new ModelValidationResult("", "{0} is invalid"));
}
return result;
}
}
それはあなたが混合され、使用
[AjaxValidation ("Validate", "Home", "Examples")]
public string Value{ get; set; }
とモデルは(また、クライアント側で使用される)を呼び出しますアクション
public ActionResult Validate(string id) {
if (id.Length == 3) {
return Json(new { Success = true });
} else {
return Json(new { Success = false });
}
}