0

我已经实现了简单的 MVC3 应用程序,我想验证控制表单特定的DIV

//视图模型

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

    }

//查看.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 的客户端验证这些下拉菜单?

4

3 回答 3

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在输入附近显示错误。

于 2013-04-02T13:32:15.127 回答
0

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

$('#addbtn').click(function(){
var ddlvalue= $("#dropdownid option:selected").val();
if(ddlvalue!='-1')
{    
   //Do your work.
}
else
  alert('Please select product");
});
于 2013-04-03T16:30:17.897 回答
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>
于 2013-05-16T13:38:52.507 回答