Using @Html.TextBoxFor(m => m.MyField)
will (normally?) result in <input id="MyField" name="MyField" type="text" value="" />
and if I want to access that element in jquery I do so as follows $("#MyField")
. Is there a way to avoid manually typing the ID into the jquery selector? It seems clunky to me to do it this way as I'm coming from a classic asp.net background where you would pull the id of the element using the ClientID property. Therefore if the renderer changed how it generated the ID ones code automatically kept up-to-date. In the above example if I change MyField to MyNewField the compiler will remind me to change all the strongly typed references, but nothing will remind me to change my jquery selector.
Asked
Active
Viewed 1,288 times
0

Dale K
- 25,246
- 15
- 42
- 71
-
can't you do @Html.TextBoxFor(m => m.MyField, new @class="someClass") then use the class as the JQuery selector instead? When working in ASP.NET our front-end developers typically only use classes as selectors as ASP generates many ID's automatically. – Christopher Marshall Jan 09 '12 at 02:25
-
Yes I could do that - although I consider that a hack since in this case I am trying to identify a unique element which already has an ID. And actually following that approach I can hard code the id in the same way if I wish. I was just hoping for an approach which allowed the automatic magic to happen consistently. For example since we have Html.LabelFor and Html.TextBoxFor which will both have consistent names what about a Html.JavascriptIdFor which will also be consistent? – Dale K Jan 09 '12 at 02:37
1 Answers
0
Write a simple method that get a property and return it's name with reflection and use it like this:
static string GetVariableName<T>(Expression<Func<T>> expression)
{
var body = expression.Body as MemberExpression;
return body.Member.Name;
}
$('#' + @(GetVariableName(() => Model.MyField)))
Should work.

gdoron
- 147,333
- 58
- 291
- 367
-
Yeah, but that doesn't guarantee me that I am using the same mechanism to generate the ID as the HTML helpers are. As I comment on my original question we have helpers to generate a label, a textbox etc all using a consistent mechanism under the hood to generate those ID's, is there not way to get the raw ID? If in MVC4 MS change the way they generate ID's then any custom method I use will stop working - which is what I am trying to avoid. – Dale K Jan 09 '12 at 02:41
-
@DaleBurrell. Don't worry. The property name will remain the generated id and name. This is how the MVC `Model Binder` works. If Microsoft will change it in the future, all the asp.net MVC developers will be in big trouble not only you. **It won't happen!** – gdoron Jan 09 '12 at 02:45
-
1haha you're probably right but it goes against my coding rules :) however I'm gonna give you the answer because it helped find this post http://stackoverflow.com/questions/3065307/client-id-for-property-asp-net-mvc which takes your suggestion AND uses the same mechanism as the other extensions. Thanks for you help. – Dale K Jan 09 '12 at 02:52