MVC 编辑器用于属性名称
有没有人写过或者有没有办法使用属性名称而不是使用表达式来调用 EditorFor 。
EditorFor("Code")
相反,
EditorFor(_ => _.Code)
我想写一些更通用的东西。通过创建表达式来调用 EditorFor 被证明是令人厌烦的,我不相信没有更好的方法。
这是行不通的。
var root = Expression.Parameter(Model.GetType(), "_");
var expr = Expression.Lambda(Expression.MakeMemberAccess(root, property), root);
var ef = (Expression<Func<Product, string>>) expr;
var method = typeof(EditorExtensions).GetMethod("EditorFor", BindingFlags.Public | BindingFlags.Static, null, new[] {typeof(HtmlHelper<>), typeof(Expression<Func<,>>)}, null);
方法总是为空,我已经尝试了很多东西。
干杯
Has anyone written or is there a way to invoke EditorFor using a property name instead of using an expression.
EditorFor("Code")
as opposed to
EditorFor(_ => _.Code)
I want to write something more generic. Invoking EditorFor by creating an expression is proving tiresome, and I cannot believe there is not a better way.
This doesn't work.
var root = Expression.Parameter(Model.GetType(), "_");
var expr = Expression.Lambda(Expression.MakeMemberAccess(root, property), root);
var ef = (Expression<Func<Product, string>>) expr;
var method = typeof(EditorExtensions).GetMethod("EditorFor", BindingFlags.Public | BindingFlags.Static, null, new[] {typeof(HtmlHelper<>), typeof(Expression<Func<,>>)}, null);
method is always null, I have tried a lot of things.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
Editor
方法就是您所追求的:http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editor.aspxI think the
Editor
method is what you are after: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editor.aspx那么,让我说清楚。您想要使用显式设计为使用表达式的方法,以便使它们成为强类型......并且您希望它不是强类型?
那么为什么不简单地使用非强类型版本呢?
“For”方法的目的是使它们成为强类型。令人困惑的是为什么您想要使用 For 类型,但不将其用于其设计用途。
So, let me get this straight. You want to use method that is explicitly designed to use expressions, for the purpose of making them strongly typed.. and you want it to not be strongly typed?
Why not simply use the non-strongly typed version then?
The purpose of the "For" methods is to make them strongly typed. It's confusing why you would want to use a For type but not use it for what's it's designed for.