Home >Web Front-end >HTML Tutorial >关于mvc中@Html.DropDownListFor和@Html.DropDownList默认值无法选中问题简单总结

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

PHP中文网
PHP中文网Original
2017-03-29 16:16:361454browse


当我们在做类似编辑功能的时候,会给定select选中默认值,然而mvc中偶尔这个功能不能用,或者是强类型的@Html.DropDownListFor不能用。凑巧今天遇到问题,解决问题时发现了mvc的一个小bug,做记录如下:

后台无论用viewbag或是viewdata都不是重点,前台无论用@Html.DropDownList  @Html.DropDownListFor都无所谓

 

重点是当后台用ViewData["typeBtnList"]=.....  或者ViewBag.typeBtnList =......前台用

@Html.DropDownList("typeBtnList", ViewData["typeBtnList"] as List
@Html.DropDownList("typeBtnList", ViewBag.typeBtnList as List

而当我们把前台改成

@Html.DropDownList("aaaaa", ViewData["typeBtnList"] as List
@Html.DropDownList("aaaaa", ViewBag.typeBtnList as List

结论:

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn