选择选项值的MVC4验证

时间:2012-10-18 07:10:38

标签: c# asp.net-mvc-4 asp.net-mvc-validation

我是MVC技术的新手,我有一个MVC 4应用程序,我正在尝试显示选择输入的验证(月和年)。我对输入类型=“文本”进行了验证,它对我来说很好。我需要帮助来选择一个选项。请帮帮忙吗?

观点:

        <li>
            <div class="requiredField">@Html.LabelFor(m => m.AccountNumber)<span>* </span>
            </div>
            @Html.TextBoxFor(m => m.AccountNumber, new {@class ="requiredTextBox" })
            <div class="swipeLink">
                @Html.ActionLink("Swipe", "Swipe", "Transaction", new { @class = "swipePopupLink" })
            </div>
        </li>
        <li>
            <div class="requiredField">@Html.Label("Expiration Date")<span>* </span></div>  
            <div class="ExpirationMonthYear">
                @Html.DropDownList("ExpirationMonth", new SelectList(Model.ExpirationMonth, "Value", "Text", 0), new { @class = "requiredTextBox" })
                @Html.DropDownList("ExpirationYear", new SelectList(Model.ExpirationYear, "Value", "Text", 0), new { @class = "requiredTextBox" })
            </div>
        </li>
@Html.ValidationSummary()

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

模型视图:

    [Required]
    [Display(Name = "Account Number")]
    public string AccountNumber { get; set; }

    [Required]        
    [Display(Name = "Expiration Date")]
    public IEnumerable<SelectListItem> ExpirationMonth { get; set; }

    [Required]
    public IEnumerable<SelectListItem> ExpirationYear { get; set; }

提前感谢您的支持

1 个答案:

答案 0 :(得分:0)

对于Drop down,您需要再绑定一个属性来检查验证

The ModelView 



 [Required]
        [Display(Name = "Account Number")]
        public string AccountNumber { get; set; }


        [Display(Name = "Expiration Date")]
        public IEnumerable<SelectListItem> ExpirationMonth { get; set; }


        public IEnumerable<SelectListItem> ExpirationYear { get; set; }

        //New property 
        [RegularExpression(@"^[^0]*$", ErrorMessage = "Select Month.")]
        public int ExpMonth{get;set;}

        [RegularExpression(@"^[^0]*$", ErrorMessage = "Select Year.")]
        public int ExpYear {get;set;}

视图

<li>
        <div class="requiredField">@Html.LabelFor(m => m.AccountNumber)<span>* </span>
        </div>
        @Html.TextBoxFor(m => m.AccountNumber, new {@class ="requiredTextBox" })
        <div class="swipeLink">
            @Html.ActionLink("Swipe", "Swipe", "Transaction", new { @class = "swipePopupLink" })
        </div>
    </li>
    <li>
        <div class="requiredField">@Html.Label("Expiration Date")<span>* </span></div>  
        <div class="ExpirationMonthYear">
            @Html.DropDownListFor(model=>model.ExpMonth, new SelectList(Model.ExpirationMonth, "Value", "Text", 0), new { @class = "requiredTextBox" })
            @Html.DropDownListFor(model=>model.ExpYear, new SelectList(Model.ExpirationYear, "Value", "Text", 0), new { @class = "requiredTextBox" })
        </div>
    </li>

注意:在上面的示例中,如果不正确,我已采用 int 数据类型,请根据您的情况采取不同的方式,然后应用验证。