views:

81

answers:

1

I'm trying to build a select list that will contain some prioritised values, then a separator, and then the rest of the values. I need to do this in a Html Helper, as I will get the values that will be prioritised and the rest of the values from different sources.

Sample of what I want to accomplish:

EUR
GBP
USD
---
SEK
ZAR
.
.
.

I also wanna make sure that the separator is not possible to select. If I do this straight in the html, I've manage to do this, but I haven't manage to disable the separator when doing this thru the helper. Any ideas how to do this?

+1  A: 

I ended up doing creating a html helper method that takes my two list, and for each item creates a new Tag of "option" type, and add it to a select list. That way I can add attributes, such as "disabled="disabled"" and so on.

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();

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.

MrW