1

我正在尝试在中制作级联下拉列表ASP.NET MVC4,我的下拉列表的两个值都来自方法,所以在如何将值从一个下拉列表传递到另一个下拉列表时遇到了麻烦。

以下是我获取第一个下拉列表值的方法:

var CampaignInfo1 = CampaignManagementService.GetAdvertisers((string)Session["ticket"]);
List<CampaignList1> items1 = new List<CampaignList1>();
foreach (var element in CampaignInfo1)
{
    items1.Add(new CampaignList1() { ID1 = element.Key, Name1 = element.Value });
}
var listOfCamp1 = new SelectList(items1, "ID1", "Name1", 1);
ViewData["list1"] = listOfCamp1;

并在视图中的下拉列表:

@Html.DropDownList("list1", ViewData["list1"] as SelectList, "-- Select Client -1-")

第二个下拉列表值得到几乎相同的方法:

var CampaignInf = CampaignManagementService.GetCampaigns((string)Session["ticket"], IDFromfirstDDL);
List<AlreadyCreatedCampaignList> itemas = new List<AlreadyCreatedCampaignList>();
foreach (var element in CampaignInf)
{
    itemas.Add(new AlreadyCreatedCampaignList() { campID = element.Key, campName = element.Value });
}
var listOfCam = new SelectList(itemas, "campID", "campName", 1);
ViewData["clist"] = listOfCam;

但是有一个问题,在 GetCampaigns 方法中,我必须传递我从第一个 DDL 获得的 id(IDFromfirstDDL),然后才返回该 id 的方法返回值。问题是我不知道如何在没有任何表单提交的情况下将选定的值从第一个 DDL 传递到第二个,因为我需要第二个 DDL 在第一个 DDL 更改后立即更改他的值。

4

1 回答 1

3

我结合http://kruisit.nl/articles/asp.net-mvc-linked-dropdown/http://www.appelsiini.net/projects/chained jquery 链式选择器这篇文章

目前在我的网站上使用

public static class LinkedDropdownHelper
        {
            #region Methods

            public static MvcHtmlString LinkedDropdownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string parent, IEnumerable<LinkedSelectListItem> selectList ,bool removedefault=false)
            {
                ModelMetadata metadata = ModelMetadata.FromLambdaExpression( expression,htmlHelper.ViewData );
                var selectedvalue = metadata.Model;
                string propertyName = metadata.PropertyName; 
                TagBuilder select = new TagBuilder("select");
                select.Attributes.Add("id", propertyName);
                select.Attributes.Add("name", propertyName);
                //select.Attributes.Add("class", "linked-dropdown");
                select.Attributes.Add("class", parent);

                foreach (var item in selectList)
                {
                    if (removedefault && item.Value == "-1")
                    {
                        //skip default
                    }
                    else
                    {
                        TagBuilder option = new TagBuilder("option");
                        option.InnerHtml = item.Text;
                        option.Attributes.Add("value", item.Value);
                        option.Attributes.Add("class", item.LinkValue);

                        if (item.Selected)
                        {
                            option.Attributes.Add("selected", "selected");
                        }
                        select.InnerHtml += option.ToString(TagRenderMode.Normal);
                    }
                }
                //below code was changed by abdurrauf to support jquery chains 
                string script = @"<script type='text/javascript'>$(document).bind('ready',  function(){
            $('#" + propertyName + "').chained('#" + parent + "');"+
                   @"$('select[name=""" + propertyName + @"""]').val("""+selectedvalue+@""");" +
                  "});</script>";

                return MvcHtmlString.Create(script + select.ToString(TagRenderMode.Normal));
            }

            #endregion Methods
        }

        public class LinkedSelectList : IEnumerable<LinkedSelectListItem>
        {
            #region Constructors

            public LinkedSelectList(IEnumerable items, string dataValueField, string dataTextField, string dataLinkedValueField, IEnumerable selectedValues)
            {
                if (items == null)
                {
                    throw new ArgumentNullException("items");
                }

                Items = items;
                DataValueField = dataValueField;
                DataTextField = dataTextField;
                DataLinkedValueField = dataLinkedValueField;
                SelectedValues = selectedValues;
            }

            #endregion Constructors

            #region Properties

            public string DataLinkedValueField
            {
                get; private set;
            }

            public string DataTextField
            {
                get; private set;
            }

            public string DataValueField
            {
                get; private set;
            }

            public IEnumerable Items
            {
                get; private set;
            }

            public IEnumerable SelectedValues
            {
                get; private set;
            }

            #endregion Properties

            #region Methods

            public virtual IEnumerator<LinkedSelectListItem> GetEnumerator()
            {
                return GetListItems().GetEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }

            internal IList<LinkedSelectListItem> GetListItems()
            {
                return GetListItemsWithValueField();
            }

            private static string Eval(object container, string expression)
            {
                object value = container;
                if (!String.IsNullOrEmpty(expression))
                {
                    value = DataBinder.Eval(container, expression);
                }
                return Convert.ToString(value, CultureInfo.CurrentCulture);
            }

            private IList<LinkedSelectListItem> GetListItemsWithValueField()
            {
                HashSet<string> selectedValues = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
                if (SelectedValues != null)
                {
                    selectedValues.UnionWith(from object value in SelectedValues select Convert.ToString(value, CultureInfo.CurrentCulture));
                }

                var listItems = from object item in Items
                                let value = Eval(item, DataValueField)
                                select new LinkedSelectListItem
                                {
                                    Value = value,
                                    Text = Eval(item, DataTextField),
                                    LinkValue = Eval(item, DataLinkedValueField),
                                    Selected = selectedValues.Contains(value)
                                };
                return listItems.ToList();
            }

            #endregion Methods
        }

        public class LinkedSelectListItem
        {
            #region Properties

            public string LinkValue
            {
                get; set;
            }

            public bool Selected
            {
                get; set;
            }

            public string Text
            {
                get; set;
            }

            public string Value
            {
                get; set;
            }

            #endregion Properties
        }
于 2013-06-19T15:37:24.460 回答