更改生成的ID和name属性 [英] Changing the generated id and name attributes

查看:148
本文介绍了更改生成的ID和name属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个动态的复杂形式的应用。
因为它的复杂性(大量的继承和复杂类型),所生成的名称和ID是大(大?)。

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?).

因此​​,我们想修改MVC正在生成的ID和name属性的方式。
我用反射器(和google / <一个href=\"http://stackoverflow.com/questions/4261323/how-editorfor-set-id-and-name-html-attributes-on-form-elements\">stackoverflow)找出在这样做了:

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:


  • 标识的使用产生:
    ViewData.TemplateInfo.GetFullHtmlFieldId(...)

  • 姓名使用产生:
    ViewData.TemplateInfo.GetFullHtmlFieldName(...)

  • 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, ...


  • 这是一个生成的ID的例子:
    <$c$c>ZForm_Part_1__Repeater_0__RepeatingPart_0__ContactSelectList_0__PersonData_TelephoneAddress_Number_FormattedNumber

  • 这是一个产生的一个例子
    名称:
    <$c$c>ZForm.Part[1].Repeater[0].RepeatingPart[0].ContactSelectList[0].PersonData.TelephoneAddress.Number.FormattedNumber

由于尺寸和我们的形式复杂这增加高达几十千字节的一个形式。结果
为什么1页?因为它是一个要求;)

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 ;)

有关现在我看到2个选项,我们怎么能更改生成这些ID的方式。我不把它们真正的,想知道是否有一个更清洁的方式做我们想要什么?结果
下面是我看到的选项:

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:


  1. 复制/粘贴的必要的辅助方法,包括私营InputHelper(...)的来源和修改InputHelper叫我们自己GetFullHtmlFieldName的版本(这是合法的吗?)

  2. 写一些过滤器,将攻入生成的HTML,并使用一些正则表达式功夫过滤掉所有id和name属性并将其等转换到一个较短的版本。

最终的结果应该产生这样的名称:

The end result should generate names like this:

Z.P[1].Rep[0].RP[0].CSL[0].PD.Tel.N.FormattedNumber

我希望我写了足够的细节来了解我们是以后的事儿。结果
理想的情况是,如果名称/ ID生成逻辑是可插拔。

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.

马努。

PS:这是因为<类似href=\"http://www.telerik.com/community/forums/aspnet-mvc/general/customizing-an-elements-form-name.aspx\"相对=nofollow>这个问题

推荐答案

您可以下载MVC源码code(从的 HTTP://aspnet.$c$cplex.com ),进行修改,编译,并引用新的程序集

You could download the MVC source code (from http://aspnet.codeplex.com), make changes, compile, and reference the new assembly.

您可能会想改变System.Web.Mvc.TemplateInfo,覆盖GetFullHtmlFieldId和GetFullHtmlFieldName方法

You'll probably want to change System.Web.Mvc.TemplateInfo, overriding the GetFullHtmlFieldId and GetFullHtmlFieldName methods

这篇关于更改生成的ID和name属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆