MrW*_*MrW 1 c# asp.net-mvc html-helper selectlist
我正在尝试构建一个包含一些优先级值的选择列表,然后是一个分隔符,然后是其余的值.我需要在Html Helper中执行此操作,因为我将获得优先级的值以及来自不同源的其余值.
我想要完成的样本:
EUR
GBP
USD
---
SEK
ZAR
.
.
.
Run Code Online (Sandbox Code Playgroud)
我还想确保无法选择分隔符.如果我直接在html中执行此操作,我已设法执行此操作,但在通过帮助程序执行此操作时,我无法禁用分隔符.任何想法如何做到这一点?
我最终创建了一个html帮助器方法,它采用了我的两个列表,并为每个项创建一个新的"选项"类型的标签,并将其添加到选择列表.这样我就可以添加属性,例如"disabled ="disabled""等.
It's not neat, it's not tidy. To be honest, it's kind of aweful code, and I would love to have a better way to do it. However, at the moment short of time to complete my task, so ended up doing this way:
var fullList = new StringBuilder();
var selectList = new TagBuilder("select");
selectList.Attributes.Add("name", "currencies");
selectList.Attributes.Add("id", "selectCurrency");
foreach (var currency in currencies)
{
var option = new TagBuilder("option") {InnerHtml = currency.Id};
option.Attributes.Add("value", currency.Id);
fullList.AppendLine(option.ToString());
}
var separator = new TagBuilder("option") { InnerHtml = "-------" };
separator.Attributes.Add("disabled", "disabled");
fullList.AppendLine(separator.ToString());
selectList.InnerHtml = fullList.ToString();
Run Code Online (Sandbox Code Playgroud)
If you have a better way, please let me know, and I'll might be able to revisit this task later on for some refactoring, and would love to have a good way to do it then.
| 归档时间: |
|
| 查看次数: |
2374 次 |
| 最近记录: |