mvc中使用remote属性来做ajax验证比较容易 :
[Remote("Action", "Controller", AdditionalFields = "Currency")]
public decimal Amount {get; set;}
生成HTML:
<input class="form-control" data-val="true" data-val-number="字段金额必须为数字" data-val-range="取款金额范围在100.00和10000.00之间。" data-val-range-max="10000.00" data-val-range-min="100.00" data-val-remote="'金额' is invalid." data-val-remote-additionalfields="*.Amount,*.Currency" data-val-remote-url="/XXX/YYY" data-val-required="请输入金额。" id="Amount" name="Amount" type="text" value="0">
也可以直接在view中写,razor在render页面时候会相应生成data-val-remote-开头的属性:
@Html.TextBoxFor(i => i.Amount,
new
{
@class = "form-control",
data_val_remote_additionalfields = "*.Amount,*.Currency",
data_val_remote_url = Url.Action("Action","Controller"),
})