【问题标题】:How to adjust the width of @Html.Dropdownlist for? The data are not showing如何调整@Html.Dropdownlist 的宽度?数据不显示
【发布时间】:2016-01-27 04:46:08
【问题描述】:

我使用@Html.DropDownLinkFor 在我的系统上选择支付选项。但是发生的事情是,它没有在下拉框中显示默认文本。当你勾选一个选项时,它仍然不会显示?如何使文本显示在下拉框中或调整其宽度?

这是我的代码

<td>
@Html.DropDownListFor(model => item.LevelId, new SelectList(item.GradeLevels, "LevelId", "LevelName", item.LevelId),
"<- Select Level ->",
new { @class = "form-control ddlClass", @id = "LevelId_" + item.TransactionId + "_" + item.StudentId})
</td>

【问题讨论】:

  • 它有多宽?并且在SelectList 中设置最后一个参数是没有意义的(你可以删除它),但是由于这似乎在一个循环内,所以它永远不会绑定到你的模型,因为你创建了多个具有相同name&lt;select&gt; 元素属性(你需要一个for 循环并使用索引器)

标签: c# asp.net-mvc asp.net-mvc-5 html.dropdownlistfor


【解决方案1】:

DropDownList 助手的第二个参数必须是 IEnumerable。

请逐步尝试。

步骤 1)

@Html.DropDownList(
"ListId", 
Enumerable.Empty<SelectListItem>(),"<- Select Level ->", 
new { style = "width: 360px;" }

)

步骤-2) 你想用来绑定的:

@Html.DropDownList(
"ListId", 
Model.PaymentMethodList,"<- Select Level ->", 
new { style = "width: 360px;" }

)

第三步)

避免在 HTML 中混杂你应该使用外部 CSS 的样式:

@Html.DropDownListFor(
    x => x.ListId, 
    Model.PaymentMethodList,"<- Select Level ->", 
    new { @class = "mydropdown" }
)

你的外部 Css 代码

.mydropdown{
width: 360px;

}

如果您仍然存在问题,请检查您的 css 和脚本或分享更多您的代码。

【讨论】:

  • 第二个参数必须是 IEnumerable&lt;SelectListItem&gt;,这正是 OP 的代码 (new SelectList(item.GradeLevels, "LevelId", "LevelName", item.LevelId))。
  • 您好我试过这个,连同更正,没有成功。唯一不同的是该列表位于引导类的跨度内:col-lg-push-4。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
相关资源
最近更新 更多