MVC3 选择列表项。是否可以用自定义 html 属性来装饰它?

发布于 2024-10-30 17:16:21 字数 719 浏览 1 评论 0原文

如果我有这样的东西:

@{
   var cats = GetCategories();
   var selectList = from c in cats
   select new SelectListItem
   {
     Selected = (c.Id == Model.SessionCategory.Id),
     Text = c.Name,
     Value = c.Id.ToString(),
    };
  }
 @Html.DropDownList("categories", selectList)

标记将是这样的:

 <select id="categories" name="categories">
    <option value="1">Category1</option>
    <option value="2">Category2</option>
    <option selected="selected" value="3">Category3</option>
 </select>

现在的问题是:

是否可以向每个 标记添加其他属性? 我的意思是来自 Razor 模板。没有 jQuery 解决方法吗?

if I have something like this:

@{
   var cats = GetCategories();
   var selectList = from c in cats
   select new SelectListItem
   {
     Selected = (c.Id == Model.SessionCategory.Id),
     Text = c.Name,
     Value = c.Id.ToString(),
    };
  }
 @Html.DropDownList("categories", selectList)

The markup would be like this:

 <select id="categories" name="categories">
    <option value="1">Category1</option>
    <option value="2">Category2</option>
    <option selected="selected" value="3">Category3</option>
 </select>

And now, the question is:

Is it possible to add additional attributes to each <option> tag?
I mean from the Razor template. Without jQuery workarounds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

熊抱啵儿 2024-11-06 17:16:21

我认为最实用的解决方案是一些 jQuery 或者类似的东西..

<select id="categories" name="categories">
    @foreach (var item in selectList) {
        <option @if (item.Selected) { <text>selected="selected"</text>} value="@item.Value">@item.Text</option>
    }
</select>

你显然可以根据需要添加属性...

I think the most practical solution would be some jQuery or maybe something like this..

<select id="categories" name="categories">
    @foreach (var item in selectList) {
        <option @if (item.Selected) { <text>selected="selected"</text>} value="@item.Value">@item.Text</option>
    }
</select>

you could obviously add in attributes as needed...

迟到的我 2024-11-06 17:16:21

默认情况下,否。 SelectListItem 不包含任何可添加属性的其他属性。但是,您可以创建自己的DropDownList方法或DropDownListFor并使用自定义创建自己的ListItemToOption SelectListItem 添加您自己的属性。

但这可能有点过分了。

By default, no. The SelectListItem does not contain any additional properties to add your attributes to. However, you could create your own DropDownList method or DropDownListFor and create your own ListItemToOption with a custom SelectListItem to add your own attributes.

But it's probably overkill.

痴者 2024-11-06 17:16:21

我是这样做的:

我创建了自己的 DropDownListFor:

    public static MvcHtmlString TheDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> listOfValues, string placeHolder)
    {
        var model = htmlHelper.ViewData.Model;
        var metaData = ModelMetadata .FromLambdaExpression(expression, htmlHelper.ViewData);

        var tb = new TagBuilder("select");

        if (listOfValues != null)
        {
            tb.MergeAttribute("id", metaData.PropertyName);
            tb.MergeAttribute("name", metaData.PropertyName);

            if (!string.IsNullOrEmpty(placeHolder))
            {
                var option = new TagBuilder("option");
                option.MergeAttribute("value", placeHolder);

                tb.InnerHtml += option.ToString();
            }

            foreach (var item in listOfValues)
            {
                var option = new TagBuilder("option");
                option.MergeAttribute("value", item.Value);

                var textNdata = item.Text.Split('|');

                option.InnerHtml = textNdata[0];

                if (textNdata.Length == 2)
                    option.MergeAttribute("data-name", textNdata[1]);

                if(item.Selected)
                    option.MergeAttribute("selected", "selected");

                tb.InnerHtml += option.ToString();
            }
        }

        return MvcHtmlString.Create(tb.ToString());
    }

然后我的 SelectListItem List 创建如下:

    public List<SelectListItem> EmployerList
    {
        get
        {
            return Employers.Select(x => new SelectListItem
            {
                Text = x.EAN + "|" + x.Name,
                Value = x.Id.ToString(),
                Selected = (SelectedEmployer != null && SelectedEmployer.Id == x.Id)
            }).ToList();
        }
    }

Here's how I did it:

I created my own DropDownListFor:

    public static MvcHtmlString TheDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> listOfValues, string placeHolder)
    {
        var model = htmlHelper.ViewData.Model;
        var metaData = ModelMetadata .FromLambdaExpression(expression, htmlHelper.ViewData);

        var tb = new TagBuilder("select");

        if (listOfValues != null)
        {
            tb.MergeAttribute("id", metaData.PropertyName);
            tb.MergeAttribute("name", metaData.PropertyName);

            if (!string.IsNullOrEmpty(placeHolder))
            {
                var option = new TagBuilder("option");
                option.MergeAttribute("value", placeHolder);

                tb.InnerHtml += option.ToString();
            }

            foreach (var item in listOfValues)
            {
                var option = new TagBuilder("option");
                option.MergeAttribute("value", item.Value);

                var textNdata = item.Text.Split('|');

                option.InnerHtml = textNdata[0];

                if (textNdata.Length == 2)
                    option.MergeAttribute("data-name", textNdata[1]);

                if(item.Selected)
                    option.MergeAttribute("selected", "selected");

                tb.InnerHtml += option.ToString();
            }
        }

        return MvcHtmlString.Create(tb.ToString());
    }

Then my SelectListItem List is created like this:

    public List<SelectListItem> EmployerList
    {
        get
        {
            return Employers.Select(x => new SelectListItem
            {
                Text = x.EAN + "|" + x.Name,
                Value = x.Id.ToString(),
                Selected = (SelectedEmployer != null && SelectedEmployer.Id == x.Id)
            }).ToList();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文