ASP.NET MVC3 DropDownList 验证问题

25 篇文章 0 订阅
22 篇文章 0 订阅

之前一直用MVC2,这几天在看MVC3,除了加入新的视图引擎外,貌似都差不太多。

不过,让哥遇到一个蛋疼的问题:DropDownList 竟然无法验证,今天特意仔细研究了下,先看测试代码
1、Model:
    public class User
    {
        [Required(ErrorMessage = "请输入姓名")]
        public string Name { get; set; }

        [Required(ErrorMessage = "请选择年龄")] 
        public int Age { get; set; }
    }

2、View
注:为测试方便,代码写在view中。

<script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

@{Html.BeginForm(null, null, FormMethod.Post, new { id = "form1" });}

@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)

<br />
@{
    Dictionary<string, string> di = new Dictionary<string, string>();
    di.Add("1", "10岁");
    di.Add("2", "20岁");
    di.Add("3", "30岁");
    ViewData["Age"] = new SelectList(di, "key", "value");
}
@Html.LabelFor(x => x.Age)
@Html.DropDownListFor(x => x.Age, ViewData["Age"] as SelectList,"--请选择--")
@Html.ValidationMessageFor(x => x.Age)

<br /><br />
<input type="submit" value="提交" />
       
@{Html.EndForm();}

3、Name、Age保持原始值,直接提交结果:


蛋疼不?Age竟然没验证?????
先fuck一下,无意中改了下view代码(注意红色部分):
...................省略.....................
@{
    Dictionary<string, string> di = new Dictionary<string, string>();
    di.Add("1", "10岁");
    di.Add("2", "20岁");
    di.Add("3", "30岁");
    ViewData[" Age2"] = new SelectList(di, "key", "value");
}
@Html.LabelFor(x => x.Age)
@Html.DropDownListFor(x => x.Age, ViewData[" Age2"] as SelectList,"--请选择--")
@Html.ValidationMessageFor(x => x.Age)
...................省略.....................

再次提交这样:


tnd,这不坑人么。。。这玩意还与ViewData变量名有关系!!!

4、继续测试,改用TempData:
...................省略.....................
@{
    Dictionary<string, string> di = new Dictionary<string, string>();
    di.Add("1", "10岁");
    di.Add("2", "20岁");
    di.Add("3", "30岁");
    TempData[" Age"] = new SelectList(di, "key", "value");
}
@Html.LabelFor(x => x.Age)
@Html.DropDownListFor(x => x.Age, TempData[" Age"] as SelectList,"--请选择--")
@Html.ValidationMessageFor(x => x.Age)
...................省略.....................

提交结果:


这是什么情况!TempData就可以,为什么ViewData就不行呢?

5、我再测试,改用ViewBag
...................省略.....................
@{
    Dictionary<string, string> di = new Dictionary<string, string>();
    di.Add("1", "10岁");
    di.Add("2", "20岁");
    di.Add("3", "30岁");
     ViewBag.Age = new SelectList(di, "key", "value");
}
@Html.LabelFor(x => x.Age)
@Html.DropDownListFor(x => x.Age,   ViewBag.Age  as SelectList,"--请选择--")
@Html.ValidationMessageFor(x => x.Age)
...................省略.....................

提交结果:


蛋又疼了,什么情况 ?

6、为ViewBag换个变量名
...................省略.....................
@{
    Dictionary<string, string> di = new Dictionary<string, string>();
    di.Add("1", "10岁");
    di.Add("2", "20岁");
    di.Add("3", "30岁");
     ViewBag.Age2 = new SelectList(di, "key", "value");
}
@Html.LabelFor(x => x.Age)
@Html.DropDownListFor(x => x.Age,   ViewBag.Age2  as SelectList,"--请选择--")
@Html.ValidationMessageFor(x => x.Age)
...................省略.....................

提交结果:


又回来了。

7、TempData、ViewData、ViewBag小结:

 

 
8、问题分析

这里的变量名Age、Age2都是与Model中的Age属性相比较的,比如:你可以测试Model改成Age2,ViewData则就是在Age有效,Age2无效了,ViewBag相同。
TempData与ViewData、ViewBag区别在于,前者是保存在Session中,后二者则不是,可能的原因在于此处,仍需继续研究啊....

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值