2

当我单击按钮客户端验证不起作用时,下面是我的视图和模型

看法:

 <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>


 @using (Html.BeginForm("Guestlogin", "Home", FormMethod.Post))
            {
                for (int i =0; i <1; i++)
                {
             @Html.DropDownListFor(per => per.[i].Gender, new[] {
                                            new SelectListItem(){Text = "Male" , Value="Male"},
                                            new SelectListItem(){Text ="Female" , Value = "Female"},
                                        }, "Select Your Gender")@Html.ValidationMessageFor(o => o[i].Gender)
            }

            <button type="submit" name="Button"/>
            }

模型:

[Required]
public string Gender { set;get;}
4

2 回答 2

1

检查你的模型:

public class urvalidationmodel
{
    [Required]
    [Display(Name = "Gender")]
    public IEnumerable<Gender> gender { get; set; }
}

js:

function Gender(sender, args) {
    var v = document.getElementById('Gender').value;
    if (v == 'Male' || v == 'Female') {
        args.IsValid = true; // Valid
    }
    else {
        args.IsValid = false; // Invalid
    }
}
于 2012-07-30T13:14:49.803 回答
0

只需在表单之前的视图中查看此代码即可:

@using (Html.BeginForm("Guestlogin", "Home", FormMethod.Post))
        {
            for (int i =0; i <1; i++)

在此之前添加以下代码:

   @{ Html.EnableClientValidation(); }
   @using (Html.BeginForm("Guestlogin", "Home", FormMethod.Post))
        {
            for (int i =0; i <1; i++)
于 2012-08-10T14:11:43.193 回答