1

I'm using this code to generate custom DropDownListFor

public static MvcHtmlString LookupFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
{
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    string lookup = metadata.PropertyName;

    IEnumerable<lookup> list = Get(lookup);
    IEnumerable<SelectListItem> items = list.Select(o => new SelectListItem
                 {
                     Text = o.title,
                     Value = o.id.ToString(),
                     Selected = o.id.Equals(metadata.Model)
                 }).ToList();

    string id = "_" + (new Random()).Next(10000);
    MvcHtmlString dropDown = htmlHelper.DropDownListFor(expression, items, "---", new {title = lookup, id});
    return dropDown;
}

is it possible to add HTML attribute to some options ?

2 Answers 2

2

You need to prefix class with @, because class is a keyword in C#:

MvcHtmlString dropDown = htmlHelper.DropDownListFor(expression, items, "---", new { @class = "cssClass", title = lookup, id});
Sign up to request clarification or add additional context in comments.

1 Comment

@Thank you simon , you right but my question is about accessing to option elements in this code.
0

I found solution for this purpose

public static HtmlString LookupFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            string lookup = metadata.PropertyName;

            IEnumerable<lookup> list = Get(lookup).ToList();
            IEnumerable<SelectListItem> items = list.Select(o => new SelectListItem
            {
                Text = o.title,
                Value = o.id.ToString(),
                Selected = o.id.Equals(metadata.Model)
            }).ToList();

            string id = "_" + (new Random()).Next(10000);
            MvcHtmlString dropDown = htmlHelper.DropDownListFor(expression, items, "---", new { title = lookup, id });

            var xml = XDocument.Parse(dropDown.ToHtmlString());
            var xml_select = xml.Element("select");
            if (xml_select != null)
            {
                var options = xml_select.Elements("option");
                foreach(var c in options )
                {
                    var val_attribute = c.Attribute("value");
                    if (val_attribute != null)
                    {
                        string value = val_attribute.Value;
                        int tmp;
                        if (Int32.TryParse(value,out tmp))
                        {
                            var fd = list.FirstOrDefault(o => o.id == Convert.ToInt32(value));
                            if (fd == null) continue;
                            if (!fd.active)
                            {
                                c.SetAttributeValue("class", "deactive-lookup");
                            }
                        }
                    }
                }
            }
            string html_string;
            using (var stringWriter = new StringWriter())
            {
                using (var xmlTextWriter = XmlWriter.Create(stringWriter))
                {
                    xml.WriteTo(xmlTextWriter);
                    xmlTextWriter.Flush();
                    html_string =  stringWriter.GetStringBuilder().ToString();
                }
            }
            return  new HtmlString(html_string);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.