using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; using Autofac; using EMIS.CommonLogic.ScoreManage; using EMIS.Utility; using Bowin.Web.Controls.Mvc; using Bowin.Common.JSON; using System.Linq.Expressions; namespace EMIS.Web.Controls.Score { public static class CreditFormularDropdownList { public static MvcHtmlString CreditFormularListFor(this HtmlHelper htmlHelper, Expression> expression, Expression> isEnableExpression, DropdownListOptions dropdownListOptions = null, IDictionary htmlAttributes = null) { List htmlList = new List(); ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); ModelMetadata isEnableMetadata = ModelMetadata.FromLambdaExpression(isEnableExpression, htmlHelper.ViewData); string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); var isEnable = (bool)(isEnableMetadata.Model ?? true); if (dropdownListOptions == null) dropdownListOptions = new DropdownListOptions(); if (string.IsNullOrEmpty(dropdownListOptions.ID)) { dropdownListOptions.ID = fullHtmlFieldName; } dropdownListOptions.Name = "combobox_" + fullHtmlFieldName; dropdownListOptions.BindType = DropdownListBindType.None; dropdownListOptions.OnSelect = "ScoreFormulaFunction.CreditFormularChangeHidden"; var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata); if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString())); using (var scope = AutofacHelper.Container.BeginLifetimeScope()) { ICreditFormulaServices creditFormulaServices = scope.Resolve(); var creditFormulaList = creditFormulaServices.GetCreditFormulaList(""); var selectedCreditFormular = creditFormulaList.FirstOrDefault(x => x.CreditFormulaID == (Guid)modelMetadata.Model); var list = creditFormulaList.Select(x => new DropdownListItem { Text = x.Name, Value = x.ToJson() }).ToList(); dropdownListOptions.ItemList = list; if (selectedCreditFormular != null) { dropdownListOptions.SelectedValue = selectedCreditFormular.ToJson(); } dropdownListOptions.IsEnabled = isEnable; htmlList.Add(htmlHelper.DropdownList(dropdownListOptions, htmlAttributes).ToString()); } htmlList.Add(htmlHelper.Hidden(fullHtmlFieldName, modelMetadata.Model, new { id = "hidden_" + fullHtmlFieldName }).ToString()); return MvcHtmlString.Create(string.Join("", htmlList)); } } }