我们正在构建一个动态的复杂表单应用程序。
由于它的复杂性(大量继承和复杂类型),生成的名称和 id 很大(巨大?)。
因此,我们想要修改 MVC 生成 id 和 name 属性的方式。
我使用了反射器(和谷歌/ stackoverflow) 找出在哪里完成的:
- ID 是使用以下方式生成的:
ViewData.TemplateInfo.GetFullHtmlFieldId(...)
- 名称是使用以下方式生成的:
ViewData.TemplateInfo.GetFullHtmlFieldName(...)
我们正在使用“...For”内置帮助器方法来生成标签、文本框、验证消息...
- 以下是生成的示例ID:
ZForm_Part_1__Repeater_0__RepeatingPart_0__ContactSelectList_0__PersonData_TelephoneAddress_Number_FormattedNumber
- 以下是生成的示例
姓名:
ZForm.Part[1].Repeater[0].RepeatingPart[0].ContactSelectList[0].PersonData.TelephoneAddress.Number.FormattedNumber
由于我们表单的大小和复杂性,这加起来有几十个一种形式的千字节。
为什么它在 1 页上?因为这是一项要求;)
目前我看到有 2 个选项可以改变这些 id 的生成方式。我真的不知道他们中的任何一个,想知道是否有一种更干净的方法来完成我们想要的事情?
以下是我看到的选项:
- 复制/粘贴必要的帮助器方法的源代码,包括私有 InputHelper(...) 并修改 InputHelper 以调用我们自己的 GetFullHtmlFieldName 版本(这合法吗?)
- 编写某种过滤器会侵入生成的 html 并使用一些正则表达式功夫来过滤掉所有 id 和 name 属性并将它们转换为较短的版本。
最终结果应该生成这样的名称:
Z.P[1].Rep[0].RP[0].CSL[0].PD.Tel.N.FormattedNumber
我希望我写了足够的细节来理解我们所追求的东西。
理想的情况是名称/ID 生成逻辑是可插入的。
马努。
ps:它类似于 这个问题。
We're building a dynamic complex form application.
Because of it's complexity (a lot of inheritance and complex types), the names and id's that are generated are big (huge?).
Therefore we would like to modify the way MVC is generating the id and name attributes.
I used reflector (and google / stackoverflow) to figure out where that's done:
- Id's are generated using:
ViewData.TemplateInfo.GetFullHtmlFieldId(...)
- Names are generated using:
ViewData.TemplateInfo.GetFullHtmlFieldName(...)
We are using the '...For' build in helper methods to generate labels, textboxes, validation messages, ...
- Here is an example of a generated id:
ZForm_Part_1__Repeater_0__RepeatingPart_0__ContactSelectList_0__PersonData_TelephoneAddress_Number_FormattedNumber
- Here is an example of a generated
name:
ZForm.Part[1].Repeater[0].RepeatingPart[0].ContactSelectList[0].PersonData.TelephoneAddress.Number.FormattedNumber
Since the size and complexity of our forms this adds up to several tens of kilobytes for one form.
Why is it on 1 page? Because it's a requirement ;)
For now I see 2 options how we could change the way these id's are generated. I don't really any of them and was wondering if there's a cleaner way to do what we want?
Here are the options I see:
- Copy/paste the source of the necessary helper methods, including the private InputHelper(...) and modify the InputHelper to call our own version of GetFullHtmlFieldName (is this legal?)
- Write some kind of filter that would hack into the generated html and use some regexp kungfu to filter out all id and name attributes and tranform them to a shorter version.
The end result should generate names like this:
Z.P[1].Rep[0].RP[0].CSL[0].PD.Tel.N.FormattedNumber
I hope I wrote enough details to understand the thing we're after.
Ideal would be if the name / id generating logic would be plugable.
Manu.
ps: It is similar as this question.
发布评论
评论(2)
您可以下载 MVC 源代码(来自 http://aspnet.codeplex.com),进行更改、编译,并引用新的程序集。
您可能想要更改 System.Web.Mvc.TemplateInfo,重写 GetFullHtmlFieldId 和 GetFullHtmlFieldName 方法
You could download the MVC source code (from http://aspnet.codeplex.com), make changes, compile, and reference the new assembly.
You'll probably want to change System.Web.Mvc.TemplateInfo, overriding the GetFullHtmlFieldId and GetFullHtmlFieldName methods
您可以使用具有符合这些要求的类和属性名称的视图模型。因为我认为,即使您找到了一种方法来修改 HTML 帮助程序生成这些属性的方式,您也可能希望在提交表单时将值绑定回某些视图模型。因此,除此之外,您还必须编写自定义模型绑定程序来处理这种情况,这可能会成为一项艰巨的工作。
You could use view models which have class and property names matching those requirements. Because I suppose that even if you find a way to modify how those attributes are generate by HTML helpers you will also want to bind values back to some view models when the form is submitted. So in addition to this you will have to write custom model binders to handle this situation which might become quite a lot of a job.