c# - 在我的 MVC3 中禁用下拉列表中的项目

标签 c# asp.net-mvc-3 razor html-select

我有一个 MVC3 应用程序。我想根据条件禁用 下拉列表 中的特定项目。

@Html.DropDownList("ReportName", Model.ReportTypes, new { style = "color:black; float:left; padding:5px;", @id = "ReportType" })
         @if (Model.numCount > 500)
         { 
          // disable the item whose value = "RC-Report";
         }

最佳答案

填充 SelectListItemExtends 列表,并传递给 ViewBag:

ViewBag.List = new Biz().ListSmall()
                .Select(s => new SelectListItemExtends()
                {
                    Text = s.dsc,
                    Value = s.id,
                    Enabled = s.is_active
                }).ToArray();

在你看来使用:

@Html.DropDownList("id", ViewBag.List as IEnumerable<SelectListItemExtends>, new { })

创建文件 HtmlHelperExtensions.cs:

using System.Collections.Generic;
using System.Web.Routing;

namespace System.Web.Mvc.Html
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItemExtends> selectList, object htmlAttributes)//, Func<object, bool> ItemDisabled)
        {
            //Creating a select element using TagBuilder class which will create a dropdown.
            TagBuilder dropdown = new TagBuilder("select");
            //Setting the name and id attribute with name parameter passed to this method.
            dropdown.Attributes.Add("name", name);
            dropdown.Attributes.Add("id", name);

            var options = "";
            TagBuilder option;
            //Iterated over the IEnumerable list.
            foreach (var item in selectList)
            {
                option = new TagBuilder("option");
                option.MergeAttribute("value", item.Value.ToString());

                if (item.Enabled == false)
                    option.MergeAttribute("disabled", "disabled");

                if (item.PropExtends != null)
                    option.MergeAttributes(item.PropExtends);

                option.SetInnerText(item.Text);
                options += option.ToString(TagRenderMode.Normal) + "\n";
            }
            //assigned all the options to the dropdown using innerHTML property.
            dropdown.InnerHtml = options.ToString();
            //Assigning the attributes passed as a htmlAttributes object.
            dropdown.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            //Returning the entire select or dropdown control in HTMLString format.
            return MvcHtmlString.Create(dropdown.ToString(TagRenderMode.Normal));

        }
    }
    public class SelectListItemExtends : SelectListItem
    {
        public bool Enabled { get; set; }
        public IDictionary<string, string> PropExtends { get; set; }
    }
}

结果是:

<select name="id" id="id">
    <option value="430">Object 1</option>
    <option value="5c7" disabled="disabled">Object 2</option>
</select>

注意

胡里奥·斯派德

wessolucoes.com.br

关于c# - 在我的 MVC3 中禁用下拉列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16531399/

相关文章:

c# - 重载导致代码膨胀

c# - 从 C# 中的另一个类调用 ConnectionString

c# - Csvhelper - 读取/获取所有行的单列?

c# - 从 Html.DropdownListFor....MVC3 获取文本

c# - 从 MVC Controller 返回 Json 但日期格式在 javascript 中不正确

asp.net-mvc-3 - .NET MVC 3 自定义十进制?模型绑定(bind)器

jquery - 使用模型渲染部分 View 时如何关闭 JQuery UI 对话框

由于 "Due to its protection level",C# 编译失败

asp.net - HtmlHelper.EditorFor与Html.TextBox与Html输入

jquery - 背景图像显示在弹出消息上