Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC 3如何验证dropdownlist_Asp.net_Asp.net Mvc 3 - Fatal编程技术网

ASP.NET MVC 3如何验证dropdownlist

ASP.NET MVC 3如何验证dropdownlist,asp.net,asp.net-mvc-3,Asp.net,Asp.net Mvc 3,我想验证生日选择部分的日期字段。我有一个下拉列表,下面列出了1-31天,默认值为0,文本为天。代码如下: public IEnumerable<SelectListItems> Date = new List<SelectListItems> { new SelectListItems{Value = "0", Text = "Day", Default = true}, new SelectListItems{Value = "

我想验证生日选择部分的日期字段。我有一个下拉列表,下面列出了1-31天,默认值为0,文本为天。代码如下:

 public IEnumerable<SelectListItems> Date = new List<SelectListItems>
    {
        new SelectListItems{Value = "0", Text = "Day", Default = true},
        new SelectListItems{Value = "1", Text = "1"},
        new SelectListItems{Value = "2", Text = "2"},
        new SelectListItems{Value = "3", Text = "3"},
        new SelectListItems{Value = "4", Text = "4"},
        new SelectListItems{Value = "5", Text = "5"},
        new SelectListItems{Value = "6", Text = "6"},
        new SelectListItems{Value = "7", Text = "7"},
        new SelectListItems{Value = "8", Text = "8"},
        new SelectListItems{Value = "9", Text = "9"},
        new SelectListItems{Value = "10", Text = "10"},
        new SelectListItems{Value = "11", Text = "11"},
        new SelectListItems{Value = "12", Text = "12"},
        new SelectListItems{Value = "13", Text = "13"},
        new SelectListItems{Value = "14", Text = "14"},
        new SelectListItems{Value = "15", Text = "15"},
        new SelectListItems{Value = "16", Text = "16"},
        new SelectListItems{Value = "17", Text = "17"},
        new SelectListItems{Value = "18", Text = "18"},
        new SelectListItems{Value = "19", Text = "19"},
        new SelectListItems{Value = "20", Text = "20"},
        new SelectListItems{Value = "21", Text = "21"},
        new SelectListItems{Value = "22", Text = "22"},
        new SelectListItems{Value = "23", Text = "23"},
        new SelectListItems{Value = "24", Text = "24"},
        new SelectListItems{Value = "25", Text = "25"},
        new SelectListItems{Value = "26", Text = "26"},
        new SelectListItems{Value = "27", Text = "27"},
        new SelectListItems{Value = "28", Text = "28"},
        new SelectListItems{Value = "29", Text = "29"},
        new SelectListItems{Value = "30", Text = "30"},
        new SelectListItems{Value = "31", Text = "31"}

    };
我的看法如下:

 public class PaxInfoViewModel
{
    public bool IsChild
    {
        get { return Age != null; }
    }

    [Required(ErrorMessage="Please select the born Month")]
    public int BirthYear1 { get; set; }
    [Range(2, 12, ErrorMessage="Please select the born Month")]
    public int BirthMonth { get; set; }
    [Required(ErrorMessage = "Please select the born Date")]
    [Range(2, int.MaxValue, ErrorMessage = "Please select the born Date")]
    public int BirthDate { get; set; }

}
 <div class="row">
                    <div class="editor-label control-label">@Html.Label("Child " + i + " Birthday ")</div>
                    <div class="span1">@Html.DropDownListFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthDate, new SelectList(Model.DropDown.Date, "Value", "Text"), new { @class = "mini" })</div>
                    <div class="span2">@Html.DropDownListFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthMonth, new SelectList(Model.DropDown.Month, "Value", "Text"), new { @class = "small" })</div>
                     <div class="errorformat">@Html.ValidationMessageFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthMonth)
                    @Html.ValidationMessageFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthDate)
                 </div></div>
@Html.DropDownListFor(Yourmodelvariable, Yourlistitems, new Dictionary<string, object>() { { "data-val-range-min", "1" }, { "data-val-range-max", "31" }, { "data-val-range", "Please select the born Date" }, { "data-val", "true" }, { "class", "mini" } }) "

@Html.ValidationMessageFor(Yourmodelvariable)
此日期需要验证。用户必须选择默认值以外的值。我该怎么做,请帮忙。
谢谢。

我终于明白了。ASP.NET MVC 3不引人注目的验证仅验证输入元素

因此,我必须对jquery.validate.unobtrusive.js代码进行一些更改

以下是我所做的:

在jquery.validate.unobtrusive.js文件中,围绕我的文件中的代码行173,您可以找到以下代码行:$selector.find:input[data val=true]。eachfunction,将其更改为:$selector.find[data val=true]。eachfunction

同样在第289行附近,您可以找到以下代码行:element=$options.form.find:input[name=+fullOtherName+][0],将其更改为:element=$options.form.find[name=+fullOtherName+][0]

在我的视图文件中,我将验证属性添加到dropdownlist,如下所示:

 public class PaxInfoViewModel
{
    public bool IsChild
    {
        get { return Age != null; }
    }

    [Required(ErrorMessage="Please select the born Month")]
    public int BirthYear1 { get; set; }
    [Range(2, 12, ErrorMessage="Please select the born Month")]
    public int BirthMonth { get; set; }
    [Required(ErrorMessage = "Please select the born Date")]
    [Range(2, int.MaxValue, ErrorMessage = "Please select the born Date")]
    public int BirthDate { get; set; }

}
 <div class="row">
                    <div class="editor-label control-label">@Html.Label("Child " + i + " Birthday ")</div>
                    <div class="span1">@Html.DropDownListFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthDate, new SelectList(Model.DropDown.Date, "Value", "Text"), new { @class = "mini" })</div>
                    <div class="span2">@Html.DropDownListFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthMonth, new SelectList(Model.DropDown.Month, "Value", "Text"), new { @class = "small" })</div>
                     <div class="errorformat">@Html.ValidationMessageFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthMonth)
                    @Html.ValidationMessageFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthDate)
                 </div></div>
@Html.DropDownListFor(Yourmodelvariable, Yourlistitems, new Dictionary<string, object>() { { "data-val-range-min", "1" }, { "data-val-range-max", "31" }, { "data-val-range", "Please select the born Date" }, { "data-val", "true" }, { "class", "mini" } }) "

@Html.ValidationMessageFor(Yourmodelvariable)
您可以在需要验证时添加validation属性在我的案例中,我想验证一个范围,它对我来说就像松散运动一样平滑