1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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<TModel>(this HtmlHelper<TModel> htmlHelper,
- Expression<Func<TModel, Guid?>> expression, Expression<Func<TModel, bool>> isEnableExpression,
- DropdownListOptions dropdownListOptions = null,
- IDictionary<string, string> htmlAttributes = null)
- {
- List<string> htmlList = new List<string>();
- ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, Guid?>(expression, htmlHelper.ViewData);
- ModelMetadata isEnableMetadata = ModelMetadata.FromLambdaExpression<TModel, bool>(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<string, string>();
- }
- validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- ICreditFormulaServices creditFormulaServices = scope.Resolve<ICreditFormulaServices>();
- 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));
- }
- }
- }
|