转换表达式< Func< T,bool?>>表达< Func< T,bool>> [英] Convert Expression<Func<T, bool?>> to Expression<Func<T, bool>>
问题描述
我有MVC视图显示一个Nullable Bool,e,g,
Html.CheckBoxFor(model => model.NullableBoolHere,Model.NullableBoolHere,
,我想创建一个新的html助手,将接受此类型,然后转换
Null || False => False
True => True
所以我有以下
public static MvcHtmlString CheckBoxFor< TModel>(此HtmlHelper< TModel> htmlHelper,Expression&FunC& ,bool?>>表达式,对象htmlAttributes,bool disabled)
{
IDictionary< string,object> values = new RouteValueDictionary(htmlAttributes);
$ b如果(禁用)
values.Add(disabled,true);
表达式< Func< TModel,bool>> boolExpression = CONVERT_TO_BOOL_HERE压力);
return htmlHelper.CheckBoxFor(expression,values);
}
任何帮助赞赏,我明白我将不得不使用递归来复制表达式,但不知道如何去浏览表达式本身,找到bool?,转换成bool。
所以,最终我找到的唯一方法是解决bool?进入一个bool自己,然后返回一个正常复选框通过传递正确的名称等。
这确实可以治疗,所以一切都好。如果你知道一个更好的方法来获取正确的ParameterName,那将是很棒的听到。
public static MvcHtmlString CheckBoxFor< TModel> ;(此HtmlHelper< TModel> htmlHelper,Expression&FunC< TModel,bool?>>表达式,对象htmlAttributes,bool disabled)
{
IDictionary< string,object> values = new RouteValueDictionary(htmlAttributes);
if(disabled)
values.Add(disabled,true);
//编译表达式以从中获取值。
var compiled = expression.Compile()。Invoke(htmlHelper.ViewData.Model);
bool checkValue = compiled.HasValue? compile.Value:false; //评估编译表达式
//获取id的名称,我们应该使用
// var parameterName =((MemberExpression)expression.Body).Member.Name; //仅给出最后一部分
string parameterName = expression.Body.ToString()。Replace(model。,); //。Replace(。,HtmlHelper.IdAttributeDotReplacement);
//返回我们的手工制作复选框
return htmlHelper.CheckBox(parameterName,checkValue,values);
}
Simple question really.
I have MVC view that displays a Nullable Bool, e,g,
Html.CheckBoxFor(model=>model.NullableBoolHere, Model.NullableBoolHere,
and I want to create a new html helper, that will accept this type, and then convert
Null || False => False
True => True
so I have the following
public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool?>> expression, object htmlAttributes, bool disabled)
{
IDictionary<string, object> values = new RouteValueDictionary(htmlAttributes);
if (disabled)
values.Add("disabled", "true");
Expression<Func<TModel, bool>> boolExpression = CONVERT_TO_BOOL_HERE(expression);
return htmlHelper.CheckBoxFor(expression, values);
}
Any help appreciated, I understand I will have to use recursion to copy the expression, but just not sure how to go about navigating the expression itself, find the bool?, convert to bool.
So, in the end, the only way I could find to do it was to resolve the bool? into a bool myself, then return a 'normal' checkbox by passing in the correct name etc.
This does work a treat though, so all good. If you do know a better way of getting the correct ParameterName, it would be great to hear.
public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool?>> expression, object htmlAttributes, bool disabled)
{
IDictionary<string, object> values = new RouteValueDictionary(htmlAttributes);
if (disabled)
values.Add("disabled", "true");
//Compile the expression to get the value from it.
var compiled = expression.Compile().Invoke(htmlHelper.ViewData.Model);
bool checkValue = compiled.HasValue ? compiled.Value : false; //evaluate the compiled expression
//Get the name of the id we should use
//var parameterName = ((MemberExpression)expression.Body).Member.Name; // only gives the last part
string parameterName = expression.Body.ToString().Replace("model.", "");//.Replace(".", HtmlHelper.IdAttributeDotReplacement);
//Return our 'hand made' checkbox
return htmlHelper.CheckBox(parameterName, checkValue, values);
}
这篇关于转换表达式< Func< T,bool?>>表达< Func< T,bool>>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!