在MVC3中使用Jquery验证DropDown

时间:2013-04-02 13:14:15

标签: jquery asp.net-mvc asp.net-mvc-3

我实现了简单的MVC3应用程序,因为我希望验证控件表单特别是 DIV

// ViewModel

public class NewStreamViewModel
    {
        public NewStreamViewModel()
        {
            this.Streams = new List<SelectListItem>();
            this.Subjects = new List<SelectListItem>();
            this.Languages = new List<SelectListItem>();
        }

        [Display(Name = "Language")]
        [Required(ErrorMessage = "{0} is Required")]
        public int? LanguageId {get;set;}


        [Display(Name = "Stream")]
        [Required(ErrorMessage = "{0} is Required")]
        public int? StreamId {get;set;}

        public int[] SubjectIds {get;set;}

        public List<SelectListItem> Languages {get;set;}
        public List<SelectListItem> Streams {get;set;}
        public List<SelectListItem> Subjects {get;set;}

    }

// View.cshtml

<div id="tab1">
@using (Html.BeginForm())
{
     <table style="background: none; width: 100%;">
                    <tr>
                        <td>
                            Language
                        </td>
                        <td>
                            @Html.DropDownListFor(m=>m.LanguageId.Value,Model.Languages)
                            @Html.ValidationMessageFor(m=>m.LanguageId.Value)
                        </td>
                    </tr>
                    <tr>
                        <td>Stream
                        </td>
                        <td>
                            @Html.DropDownListFor(m => m.StreamId.Value, Model.Streams)
                             @Html.ValidationMessageFor(m=>m.StreamId.Value)
                        </td>
                    </tr>
                    <tr>
                        <td>Subjects
                        </td>
                        <td>
                            @Html.ListBoxFor(m => m.SubjectIds, Model.Subjects, new { Style = "width:300px;" })
                        </td>
                    </tr>

                </table>
}
    <input type="button" value="Save" id="addspan" />
</div>  



<script type="text/javascript">
    $(function () {
        var count = 2;
        $('#wrap').tabs();
        $('#addspan').click(function () {
            var $step = $('#tab1');
            var validator = $("form").validate(); // obtain validator
            var anyError = false;
            var selects = $('#tab1').find('select');
            selects.each(function () {
                if (!validator.element(this)) { // validate every input element inside this step
                    anyError = true;
                }

            });
            if (anyError) {
                return false;
            }

            count++;
        });
    });
</script>

我如何在jquery的客户端验证这些Drop-Downs?

3 个答案:

答案 0 :(得分:1)

将此文件添加到您的页面(这将允许客户端验证):

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

通话:

$('form').valid()

如果没有错误,将返回true,如果是,则返回false并在输入附近显示错误。

答案 1 :(得分:0)

您可以像这样手动验证下拉列表:

$('#addbtn').click(function(){
var ddlvalue= $("#dropdownid option:selected").val();
if(ddlvalue!='-1')
{    
   //Do your work.
}
else
  alert('Please select product");
});

答案 2 :(得分:0)

  <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $('#<%=btnSubmit.ClientID%>').click(function () {
                if ($('#<%=ddlCity.ClientID%>').val() == 0) {
                    alert('Please select Country')
                    return false;
                   }                              
                if ($('#<%=txtName.ClientID%>').val() == "") {
                    alert(' Name')
                    return false;
                }

            });
        });  

    </script>