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 GradePointFormularDropdownList { public static MvcHtmlString GradePointFormularListFor(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.GradePointFormularChangeHidden"; 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()) { IGradePointFormulaServices gradePointFormulaServices = scope.Resolve(); var gradePointFormulaList = gradePointFormulaServices.GetGradePointFormulaList(""); var selectedGradePointFormular = gradePointFormulaList.FirstOrDefault(x => x.GradePointFormulaID == (Guid)modelMetadata.Model); var list = gradePointFormulaList.Select(x => new DropdownListItem { Text = x.Name, Value = x.ToJson() }).ToList(); dropdownListOptions.ItemList = list; if (selectedGradePointFormular != null) { dropdownListOptions.SelectedValue = selectedGradePointFormular.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)); } } }