asp.net-mvc – HTMLHelper,从属性中生成“Expression> expre
我正在为编辑器编写
HTML Helper.我们的想法是使用属性AutoGenerateField从Model获取属性并构建一个表,其中每行包含一个字段的名称(也来自属性)和一个包含字段实际值的TextBox或CheckBox.
我有HTMLHelper的问题.由于我将整个模型发送给助手而不是一个值,因此我不能使用TextBoxFor等方法,因为它们需要参数,例如 "Expression<Func<TModel,TValue>> expression". 我正在使用反射,我尝试发送属性,但VisualStudio认为这是不正确的用法. 下面是我的HtmlHelper的简化方法: public static MvcHtmlString GenerateEditor<TModel>(this HtmlHelper<TModel> htmlHelper) { var model = htmlHelper.ViewData.Model; var result = String.Empty; //generating container,etc ... foreach (var property in model.GetType().GetProperties()) { var attr = property.GetCustomAttributes(typeof (DisplayAttribute),true).FirstOrDefault(); if (attr == null) continue; var autoGenerate = ((DisplayAttribute)attr).AutoGenerateField; if(autoGenerate) { //here I'm building the html string //My problem is in the line below: var r = htmlHelper.TextBoxFor(property); } } return MvcHtmlString.Create(result); } 有任何想法吗? 解决方法
如何使用非lambda重载. :InputExtensions.TextBox()
if(autoGenerate) { //here I'm building the html string //My problem is in the line below: var r = htmlHelper.TextBox(property.Name); } //not sure what you do with r from here... 如果我没有弄错,表单元素的name属性设置为属性名称,即使你使用函数的lambda版本,所以这应该做同样的事情. 我将尝试验证lambda函数的作用,您可能也可以这样做,因为您有TModel. 更新 从快速查看源代码内的事物InputExtensions.cs开始,TextBoxFor调用最终调用InputHelper(),最终调用ExpressionHelper.cs内的ExpressionHelper.GetExpressionText(LambdaExpression表达式),从粗略看起来的东西获取名称html属性的member.Name输入元素. 我现在无法完全验证它,因为我不在Windows上,但我认为非lambda函数应该适合您的需要.请告诉我它是怎么回事? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 如何通过eBay API获取PayPal TransactionID
- asp.net – 用JavaScript计算时间间隔
- asp.net-mvc – FineUploader无法POST
- asp.net – 在使用ASPNet Core的Linux上托管时的Windows身份
- asp.net – 将Crystal报表导出到文本文件 – 没有RTF文件
- asp.net – 将http状态代码设置为XXX.X?
- asp.net-mvc – ASP.NET MVC WebSite中的ERR_EMPTY_RESPONS
- asp.net-mvc – 创建/编辑父和子窗体MVC
- asp.net-mvc – 如何使用ASP.NET MVC在我的现有数据库中设置
- asp.net – 您实际可以拥有多少应用程序会话数据?
- asp.net – Web.config自定义错误模式冲突
- asp.net – 在Global.asax文件中创建Timer
- 强制ASP.NET GridView的寻呼机显示
- asp.net-mvc – asp.net mvc3 jquery ui对话框和
- asp.net – TransactionScope是否需要DTC服务?
- asp.net – MVC 6 WebAPI返回序列化的HttpRespon
- asp.net-mvc-4 – 自定义多态模型绑定程序不绑定
- asp.net-mvc-5 – ASP.NET标识 – 什么方法创建表
- asp.net-mvc – asp.net mvc应用程序中的Web浏览
- attributes – 利用属性版本控制在Swagger中利用