mvc中使用remote属性来做ajax验证

mvc中使用remote属性来做ajax验证比較easy :


  [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"),		    
                        })


posted @ 2017-06-12 17:06  wzjhoutai  阅读(535)  评论(0编辑  收藏  举报