0

我是 MVC 技术的新手,我有一个 MVC 4 应用程序,正在尝试显示对选择输入(月份和年份)的验证。我对 input type="text" 进行了验证,它对我来说工作正常。我需要帮助来选择一个选项。请帮忙解决这个问题?

风景:

        <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; }

感谢您的支持

4

1 回答 1

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数据类型,如果它不正确,请根据您的情况采用不同的数据类型,然后应用验证。

于 2012-10-18T07:32:53.180 回答