using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using EntityProject;
namespace HelpeProject
{
public static class myHtmlHelper
{
public static MvcHtmlString HtmlAddressFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression1, Expression<Func<TModel, TProperty>> expression2, Expression<Func<TModel, TProperty>> expression3, object htmlAttributes = null)
{
StringBuilder str = new StringBuilder();
str.Append("<script src=\"/Content/scripts/ViewJS/JsProduct.js\"></script>");
str.Append("<script>$(document).ready(function () {BindAddress(\"" + GetPropertyInfoArray(htmlHelper, expression1) + "\",\"" + GetPropertyInfoArray(htmlHelper, expression2) + "\",\"" + GetPropertyInfoArray(htmlHelper, expression3) + "\");});</script>");
str.Append(myHtmlAddress(htmlHelper, expression1, htmlAttributes));
str.Append(myHtmlAddress(htmlHelper, expression2, htmlAttributes));
str.Append(myHtmlAddress(htmlHelper, expression3, htmlAttributes));
return new MvcHtmlString(str.ToString());
}
private static MvcHtmlString myHtmlAddress<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes=null)
{
//获取lambda表达式的的结果值(属性值)
var value = getExpression_Value(htmlHelper,expression);
var objList=Select_Item_List(value.ToString());
return htmlHelper.DropDownListFor<TModel, TProperty>(expression, objList, htmlAttributes);
}
private static string GetPropertyInfoArray<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
//获取属性的名称
var exp = expression.Body as MemberExpression;
string expStr = exp.ToString();//形如 m.name
string id = expStr.Substring(expStr.IndexOf(".") + 1);
return id;
}
private static object getExpression_Value<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TProperty>> expression)
{ /*
* 参数1:
this HtmlHelper<TModel> htmlHelper中的TModel,是存放所有页面数据的(包含:viewbag,viewdata,视图model)载体,其实视图model和viewdata是放一起的
此时,如果打断点,可发现,lambda的结果已经计算好了.存放在ViewData里面,我们可以通过元数据,查询拉姆达表达式获取结果值
*参数2
Expression<Func<TModel, TProperty>> expression 是拉姆达表达式(匿名函数,委托的变形),TModel是传入的数据类型一般指视图模型(eg: m=>m.id ) TProperty
是返回的类型.
拉姆达表达式:expression.Body 是箭头左侧的那一段
MemberExpression 表示访问字段或属性。可以通过其获取字段的名称
*/
//获取属性的名称
var exp = expression.Body as MemberExpression;
string expStr = exp.ToString();//形如 m.name
string id = expStr.Substring(expStr.IndexOf(".") + 1);
//获取lambda表达式的的结果值(属性值)
var value = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model;
return value;
}
/// <summary>
/// 获取 元素列表 值
/// </summary>
/// <param name="areaNumber"></param>
/// <returns></returns>
private static IEnumerable<SelectListItem> Select_Item_List(string areaNumber)
{
List<SelectListItem> list = new List<SelectListItem>();
using (EFDbContext context = new EFDbContext())
{
var cModel = context.Tb_Area.SingleOrDefault(c => c.AreaId.Equals(areaNumber));
SelectListItem model = null;
model = new SelectListItem();
model.Text = "--请选择--";
model.Value = "";
model.Selected = true;
list.Add(model);
if (cModel == null) return list;
var objList = context.Tb_Area.Where(c => c.ParentId.Equals(cModel.ParentId));
foreach (var item in objList)
{
model = new SelectListItem();
model.Text = item.AreaName;
model.Value = item.AreaId;
if (item.AreaId.Equals(areaNumber))
model.Selected = true;
else
model.Selected = false;
list.Add(model);
}
}
return list;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using EntityProject;
namespace HelpeProject
{
public static class myHtmlHelper
{
public static MvcHtmlString HtmlAddressFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression1, Expression<Func<TModel, TProperty>> expression2, Expression<Func<TModel, TProperty>> expression3, object htmlAttributes = null)
{
StringBuilder str = new StringBuilder();
str.Append("<script src=\"/Content/scripts/ViewJS/JsProduct.js\"></script>");
str.Append("<script>$(document).ready(function () {BindAddress(\"" + GetPropertyInfoArray(htmlHelper, expression1) + "\",\"" + GetPropertyInfoArray(htmlHelper, expression2) + "\",\"" + GetPropertyInfoArray(htmlHelper, expression3) + "\");});</script>");
str.Append(myHtmlAddress(htmlHelper, expression1, htmlAttributes));
str.Append(myHtmlAddress(htmlHelper, expression2, htmlAttributes));
str.Append(myHtmlAddress(htmlHelper, expression3, htmlAttributes));
return new MvcHtmlString(str.ToString());
}
private static MvcHtmlString myHtmlAddress<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes=null)
{
//获取lambda表达式的的结果值(属性值)
var value = getExpression_Value(htmlHelper,expression);
var objList=Select_Item_List(value.ToString());
return htmlHelper.DropDownListFor<TModel, TProperty>(expression, objList, htmlAttributes);
}
private static string GetPropertyInfoArray<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
//获取属性的名称
var exp = expression.Body as MemberExpression;
string expStr = exp.ToString();//形如 m.name
string id = expStr.Substring(expStr.IndexOf(".") + 1);
return id;
}
private static object getExpression_Value<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TProperty>> expression)
{ /*
* 参数1:
this HtmlHelper<TModel> htmlHelper中的TModel,是存放所有页面数据的(包含:viewbag,viewdata,视图model)载体,其实视图model和viewdata是放一起的
此时,如果打断点,可发现,lambda的结果已经计算好了.存放在ViewData里面,我们可以通过元数据,查询拉姆达表达式获取结果值
*参数2
Expression<Func<TModel, TProperty>> expression 是拉姆达表达式(匿名函数,委托的变形),TModel是传入的数据类型一般指视图模型(eg: m=>m.id ) TProperty
是返回的类型.
拉姆达表达式:expression.Body 是箭头左侧的那一段
MemberExpression 表示访问字段或属性。可以通过其获取字段的名称
*/
//获取属性的名称
var exp = expression.Body as MemberExpression;
string expStr = exp.ToString();//形如 m.name
string id = expStr.Substring(expStr.IndexOf(".") + 1);
//获取lambda表达式的的结果值(属性值)
var value = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model;
return value;
}
/// <summary>
/// 获取 元素列表 值
/// </summary>
/// <param name="areaNumber"></param>
/// <returns></returns>
private static IEnumerable<SelectListItem> Select_Item_List(string areaNumber)
{
List<SelectListItem> list = new List<SelectListItem>();
using (EFDbContext context = new EFDbContext())
{
var cModel = context.Tb_Area.SingleOrDefault(c => c.AreaId.Equals(areaNumber));
SelectListItem model = null;
model = new SelectListItem();
model.Text = "--请选择--";
model.Value = "";
model.Selected = true;
list.Add(model);
if (cModel == null) return list;
var objList = context.Tb_Area.Where(c => c.ParentId.Equals(cModel.ParentId));
foreach (var item in objList)
{
model = new SelectListItem();
model.Text = item.AreaName;
model.Value = item.AreaId;
if (item.AreaId.Equals(areaNumber))
model.Selected = true;
else
model.Selected = false;
list.Add(model);
}
}
return list;
}
}
}