CreditFormularDropdownList.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Mvc.Html;
  7. using Autofac;
  8. using EMIS.CommonLogic.ScoreManage;
  9. using EMIS.Utility;
  10. using Bowin.Web.Controls.Mvc;
  11. using Bowin.Common.JSON;
  12. using System.Linq.Expressions;
  13. namespace EMIS.Web.Controls.Score
  14. {
  15. public static class CreditFormularDropdownList
  16. {
  17. public static MvcHtmlString CreditFormularListFor<TModel>(this HtmlHelper<TModel> htmlHelper,
  18. Expression<Func<TModel, Guid?>> expression, Expression<Func<TModel, bool>> isEnableExpression,
  19. DropdownListOptions dropdownListOptions = null,
  20. IDictionary<string, string> htmlAttributes = null)
  21. {
  22. List<string> htmlList = new List<string>();
  23. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, Guid?>(expression, htmlHelper.ViewData);
  24. ModelMetadata isEnableMetadata = ModelMetadata.FromLambdaExpression<TModel, bool>(isEnableExpression, htmlHelper.ViewData);
  25. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  26. var isEnable = (bool)(isEnableMetadata.Model ?? true);
  27. if (dropdownListOptions == null)
  28. dropdownListOptions = new DropdownListOptions();
  29. if (string.IsNullOrEmpty(dropdownListOptions.ID))
  30. {
  31. dropdownListOptions.ID = fullHtmlFieldName;
  32. }
  33. dropdownListOptions.Name = "combobox_" + fullHtmlFieldName;
  34. dropdownListOptions.BindType = DropdownListBindType.None;
  35. dropdownListOptions.OnSelect = "ScoreFormulaFunction.CreditFormularChangeHidden";
  36. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  37. if (htmlAttributes == null)
  38. {
  39. htmlAttributes = new Dictionary<string, string>();
  40. }
  41. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  42. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  43. {
  44. ICreditFormulaServices creditFormulaServices = scope.Resolve<ICreditFormulaServices>();
  45. var creditFormulaList = creditFormulaServices.GetCreditFormulaList("");
  46. var selectedCreditFormular = creditFormulaList.FirstOrDefault(x => x.CreditFormulaID == (Guid)modelMetadata.Model);
  47. var list = creditFormulaList.Select(x => new DropdownListItem { Text = x.Name, Value = x.ToJson() }).ToList();
  48. dropdownListOptions.ItemList = list;
  49. if (selectedCreditFormular != null)
  50. {
  51. dropdownListOptions.SelectedValue = selectedCreditFormular.ToJson();
  52. }
  53. dropdownListOptions.IsEnabled = isEnable;
  54. htmlList.Add(htmlHelper.DropdownList(dropdownListOptions, htmlAttributes).ToString());
  55. }
  56. htmlList.Add(htmlHelper.Hidden(fullHtmlFieldName, modelMetadata.Model, new { id = "hidden_" + fullHtmlFieldName }).ToString());
  57. return MvcHtmlString.Create(string.Join("", htmlList));
  58. }
  59. }
  60. }