client validation

首先要引用validation.js

[Required(ErrorMessage = "An Album Title is required")]
[StringLength(160)]
public string Title { get; set; }
[Required(ErrorMessage = "Price is required")]
[Range(0.01, 100.00,
ErrorMessage = "Price must be between 0.01 and 100.00")]
public decimal Price { get; set; }

 

@model MvcApplication1.Models.Order
 @using (Html.BeginForm()) { 
   
    <fieldset>
        <legend>ClassA</legend>

        
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Total)
            @Html.ValidationMessageFor(model => model.Total)
        </div>    
        
         <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>     
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
    @Html.ValidationSummary(true)

 }

 

<script type="text/javascript">
    $.validator.unobtrusive.adapters.addSingleVal("maxwords", "wordcount");
    $.validator.addMethod("maxwords", function (value, element, maxwords) {
        if (value) {
            if (value.split(' ').length > maxwords) {
                return false;
            }
            return true;
        }
    });
</script>

 

posted on 2013-03-05 15:13  fishyk  阅读(141)  评论(0编辑  收藏  举报

导航