[C#][MVC]DropDownListFor 默认值无法选中的 BUG

本文来自:https://www.cnblogs.com/craze/p/6124575.html

关于mvc中@Html.DropDownListFor和@Html.DropDownList默认值无法选中问题简单总结

 

结论:

  无法绑定默认值是微软MVC的一个BUG

触发条件:

  整个Controller中ViewBag或者ViewData构造的参数别名有与DropDownListFor和DropDownList构造的页面标签名字相同时出现。

  注意是整个Controller中ViewBag和ViewData对象的所有参数,并不是赋值为SelectList的参数

例子:

  错误:不能如愿选中默认值  

1
@Html.DropDownList( "Filed1" , ViewBag.Filed1  as  SelectList,  new  { @ class  "form-control"  })
1
@Html.DropDownListFor(model=>model.Filed1, ViewBag.Filed1  as  SelectList,  new  { @ class  "form-control"  })

  正确:可以如愿选中默认值

1
@Html.DropDownList( "Filed1" , ViewBag.temp  as  SelectList,  new  { @ class  "form-control"  })
1
@Html.DropDownListFor(model=>model.Filed1, ViewBag.temp  as  SelectList,  new  { @ class  "form-control"  }) 

  结论:

  参数命名很重要,因为看不到源码,故猜测问题便是名称问题。

转载于:https://www.cnblogs.com/z5337/p/8397766.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值