【问题标题】:DropDownList in mvc3 don't recognize selecteditem in selectlistmvc3 中的 DropDownList 无法识别选择列表中的选定项
【发布时间】:2012-03-19 08:33:12
【问题描述】:

我正在尝试通过 ViewBag 发送 SelectedList 以查看,并且在控制器上我已将所选项目设置为控制器

   ViewData["aaa"]= new SelectList(new []{new SelectListItem { Text = "1", Value = "1" },
 new SelectListItem { Text = "2", Value = "2"},
 new SelectListItem { Text = "25", Value = "25" },
 new SelectListItem { Text = "50", Value = "50" },
 new SelectListItem { Text = "100", Value = "100" },
 new SelectListItem { Text = "1000", Value = "1000" }}, "Text", "Value", "100" );

查看部分

 @Html.DropDownListFor(m1 => m1.Sobe,(IEnumerable<SelectListItem>)ViewData["aaa"])

我得到了这个 html

    <select data-val="true" data-val-number="The field Sobe must be a number." data-val-required="The Sobe field is required." id="Sobe" name="Sobe"><option value="1">1</option>
<option value="2">2</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="1000">1000</option>
</select>

这意味着没有任何选择。没看懂,求帮助

【问题讨论】:

  • 我知道如果我将 DropDownListFor 更改为 DropDownLIst 并从属性中设置不同的名称,它会起作用,但是我无法取回值

标签: text new-operator selectlist selectlistitem


【解决方案1】:

Html 助手选择在

中找到的项目

m1.Sobe

。如果 m1.Sobe 为 0,则不会选择任何内容。

Html 助手忽略“SelectList”中的选定项

【讨论】:

  • 并非如此,我只是明确定义了该值。我找到了更改下拉列表名称的解决方案,但我不能将数据绑定到 html?有什么解决办法吗?
  • 名称必须相同才能将数据返回到模型
  • 是的,我知道,但是如果我将名称与属性相同,mvc 将不会显示选定的值。这是文章mattpenner.info/2010/02/02/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
相关资源
最近更新 更多