/// <summary> /// SiIdNameTextBoxFor /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="htmlHelper"></param> /// <param name="expression"></param> /// <param name="htmlAttributes"></param> /// <returns></returns> public static MvcHtmlString SiIdNameTextBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, object htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string inferredName = ExpressionHelper.GetExpressionText(expression); var idExpr = LambdaHelper.BuildPropertySpecifier <TModel, Nullable <int> >(string.Format("{0}Id", inferredName)); var nameExpr = LambdaHelper.BuildPropertySpecifier <TModel, string>(string.Format("{0}.Name", inferredName)); //string resolvedLabelText = metadata.DisplayName ?? metadata.PropertyName ?? inferredName; return(SiField(htmlHelper, expression, null, new List <MvcHtmlString>() { htmlHelper.HiddenFor(idExpr), htmlHelper.TextBoxFor(nameExpr, htmlAttributes) })); }
/// <summary> /// SiDropDownListFor /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="htmlHelper"></param> /// <param name="expression"></param> /// <param name="list"></param> /// <returns></returns> public static MvcHtmlString SiDropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <TProperty> list) { string inferredName = ExpressionHelper.GetExpressionText(expression); var code = LambdaHelper.BuildPropertySpecifier <TProperty, int>(string.Format("{0}.Id", inferredName)); var name = LambdaHelper.BuildPropertySpecifier <TProperty, string>(string.Format("{0}.Name", inferredName)); var v = UtilHelper.GetFieldValue(htmlHelper.ViewData.Model, string.Format("{0}Id", inferredName)); Mapper.CreateMap <TProperty, SelectListItem>() .ForMember(dest => dest.Text, opt => opt.MapFrom(name)) .ForMember(dest => dest.Value, opt => opt.MapFrom(code)); var selectList = Mapper.Map <IEnumerable <TProperty>, IEnumerable <SelectListItem> >(list); var selected = selectList.Where(p => p.Value == v.ToString()).SingleOrDefault(); if (selected != null) { selected.Selected = true; } return(SiDropDownListFor(htmlHelper, expression, selectList)); }
/// <summary> /// SiUControlFor /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="htmlHelper"></param> /// <param name="expression"></param> /// <returns></returns> public static MvcHtmlString SiUControlFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression) { string inferredName = ExpressionHelper.GetExpressionText(expression); var idExpr = LambdaHelper.BuildPropertySpecifier <TModel, Nullable <int> >(string.Format("{0}Id", inferredName)); var nameExpr = LambdaHelper.BuildPropertySpecifier <TModel, string>(string.Format("{0}.Name", inferredName)); string controllerName = inferredName.EndsWith("y") ? string.Format("{0}ies", inferredName.Substring(0, inferredName.Length - 1)) : string.Format("{0}s", inferredName); return(SiField(htmlHelper, expression, null, new List <MvcHtmlString> { htmlHelper.HiddenFor(idExpr), htmlHelper.TextBoxFor(nameExpr, new Dictionary <string, object>() { { "class", "search-text" } }), htmlHelper.SiLink("", "", new Dictionary <string, object>() { { "class", "si-btn search" } }) }, new { controller = controllerName } )); }