Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Globalization;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Mvc.Html;
- using System.Web.Mvc.Resources;
- using System.Web.Routing;
- namespace yourNamespace
- {
- /// <summary>
- /// Dropfiller is a drop down menu that will auto fill other fields based on it's selected value
- /// </summary>
- public static class DropFiller
- {
- internal static IDictionary<string, object> ObjectToDictionary(object instance)
- {
- IDictionary<string, object> propertyDictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
- if (instance != null)
- {
- foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(instance))
- {
- object obj2 = descriptor.GetValue(instance);
- propertyDictionary.Add(descriptor.Name, obj2);
- }
- }
- return propertyDictionary;
- }
- public static int EditorForIndex(this HtmlHelper htmlHelper)
- {
- var start = htmlHelper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix.LastIndexOf("[", StringComparison.Ordinal) + 1;
- var end = htmlHelper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix.LastIndexOf("]", StringComparison.Ordinal);
- var strNumber = htmlHelper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix.Substring(start, end - start);
- int ret;
- if (int.TryParse(strNumber,out ret))
- {
- return ret;
- }
- return -1;
- }
- public static MvcHtmlString Idify(this HtmlHelper htmlHelper,string id)
- {
- return new MvcHtmlString(TagBuilder.CreateSanitizedId(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(id)));
- }
- public static MvcHtmlString GetDropDownFillerForTagID<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TProperty>> expression)
- {
- var name = ExpressionHelper.GetExpressionText(expression);
- return new MvcHtmlString(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(name));
- }
- public static MvcHtmlString DropDownFillerFor<TModel, TProperty, T2Model>(this HtmlHelper<TModel> htmlHelper,
- Expression<Func<TModel, TProperty>> expression,
- IEnumerable<T2Model> objectList,
- Expression<Func<T2Model, string>> valueExp,
- Expression<Func<T2Model, string>> textExp,
- List<SelectListItem> optionLabels,
- object htmlAttributes)
- {
- return htmlHelper.DropDownFillerFor(expression, objectList, valueExp, textExp, optionLabels, ObjectToDictionary(htmlAttributes));
- }
- public static MvcHtmlString DropDownFillerFor<TModel, TProperty, T2Model>(this HtmlHelper<TModel> htmlHelper,
- Expression<Func<TModel, TProperty>> expression,
- IEnumerable<T2Model> objectList,
- Expression<Func<T2Model, string>> valueExp,
- Expression<Func<T2Model, string>> textExp,
- List<SelectListItem> optionLabels = null,
- IDictionary<string, object> htmlAttributes = null)
- {
- var textFunc = textExp.Compile();
- var valueFunc = valueExp.Compile();
- var name = ExpressionHelper.GetExpressionText(expression);
- string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
- string localname = fullName.Substring(htmlHelper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix.Length + 1);
- if (String.IsNullOrEmpty(fullName))
- {
- throw new ArgumentException("Name Error", "expression");
- }
- var defaultValue = GetModelStateValue(localname, typeof(string), htmlHelper);
- if (defaultValue == null)
- {
- defaultValue = string.Empty;
- }
- //object defaultValue = htmlHelper.GetModelStateValue(fullName, typeof(string));
- // Convert each ListItem to an <option> tag
- StringBuilder listItemBuilder = new StringBuilder();
- var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
- if (optionLabels != null)
- {
- foreach (var item in optionLabels)
- {
- TagBuilder builder = new TagBuilder("option")
- {
- InnerHtml = item.Text.Trim()
- };
- builder.Attributes["value"] = item.Value;
- if (builder.Attributes["value"] == defaultValue.ToString())
- {
- item.Selected = true;
- }
- if (item.Selected)
- {
- builder.Attributes["selected"] = "selected";
- }
- listItemBuilder.AppendLine(builder.ToString(TagRenderMode.Normal));
- }
- }
- foreach (var o in objectList)
- {
- TagBuilder builder = new TagBuilder("option")
- {
- InnerHtml = HttpUtility.HtmlEncode(textFunc(o))
- };
- builder.Attributes["value"] = valueFunc(o);
- if (builder.Attributes["value"] == defaultValue.ToString())
- {
- builder.Attributes["selected"] = "selected";
- }
- builder.Attributes["data-jsonencoded"] = jsonSerializer.Serialize(o);
- listItemBuilder.AppendLine(builder.ToString(TagRenderMode.Normal));
- }
- TagBuilder tagBuilder = new TagBuilder("select")
- {
- InnerHtml = listItemBuilder.ToString()
- };
- tagBuilder.MergeAttributes(htmlAttributes);
- tagBuilder.MergeAttribute("name", fullName, true /* replaceExisting */);
- tagBuilder.GenerateId(fullName);
- tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name));
- return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
- }
- internal static string ListItemToOption(SelectListItem item)
- {
- TagBuilder builder = new TagBuilder("option")
- {
- InnerHtml = HttpUtility.HtmlEncode(item.Text)
- };
- if (item.Value != null)
- {
- builder.Attributes["value"] = item.Value;
- }
- if (item.Selected)
- {
- builder.Attributes["selected"] = "selected";
- }
- return builder.ToString(TagRenderMode.Normal);
- }
- internal static object GetModelStateValue<TModel>(string key, Type destinationType, HtmlHelper<TModel> htmlHelper)
- {
- ModelState modelState;
- if (htmlHelper.ViewContext.ViewData.ModelState.TryGetValue(key, out modelState))
- {
- if (modelState.Value != null)
- {
- return modelState.Value.ConvertTo(destinationType, null /* culture */);
- }
- }
- return htmlHelper.ViewContext.ViewData.Eval(key);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment