7

I am loving ASP.NET MVC, keeping up with the releases/docs can sometimes be tricky, so maybe I'm just not getting something... I want to use a TextBoxFor(), and working with LabelFor() etc. is fine, all the magic happens for me.

But if I create...

 <%=Html.TextBoxFor(x => x.LastName) %>

And wanted to do something nice with jQuery, how would I get the ID of the control that was created? I could add a CSS class and use that to attach my jQuery, but for something I am doing I would like the ID... so I could do something like:

 $('#LastName').(...)

I know I could work it out in this case, and hack it in manually, but is there a neater way?

Colin Asquith
  • 653
  • 1
  • 6
  • 13
  • 1
    Does this not work? When I try it, it seems to generate the ID as I'd expect it to be named. When you view the HTML source on the page, what is your `TextBoxFor()` outputting? – Amadiere Mar 24 '10 at 17:56
  • This does work and generates the ID that you would expect, but I think that if the framework can work it out for a textbox, it would be able to work it out in other places... Especially useful when the ID is being generated to an array so you end up with an ID like "Websites_0__Url". The manual way works, I just think MVC must know this for me and saves me making mistakes ;) – Colin Asquith Mar 25 '10 at 10:55

4 Answers4

12

I think you can do something like:

<%=Html.TextBoxFor(x => x.LastName, new { id = "LastName" })%>

Overloads of TextBoxFor

bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • This would work in most cases, and I think I had the link between ID and Name too firm in my head, of course I could modify it! I think it would have issues when it was an array that MVC worked out the control IDs for and you ended up with etc. – Colin Asquith Mar 25 '10 at 11:07
  • Another way to do it, is to use MvcContrib, which introduces the IdFor method, which lets you do <%=this.IdFor(x => x.LastName)%> which is magical! – Colin Asquith Apr 14 '10 at 13:28
  • Instead of generating the field identifiers manually I would recommend Colin's approach or if you don't want to depend on MVCContrib then http://stackoverflow.com/questions/3065307/client-id-for-property-asp-net-mvc – JCallico Apr 18 '11 at 19:08
7

Since MVC4 there is a built-in way to do it - @Html.IdFor().

Here is a sample of using it:

@Html.IdFor(m => m.Filters.Occurred.From)

and the result is like

Filters_Occurred_From
Alex Klaus
  • 8,168
  • 8
  • 71
  • 87
0

As a point of interest it appears that the Html.Textbox() code will generate an id, duplicating the control name for anything that begins with a letter (a-z). If however your 'name' begins with a number it will simply not bother.

This is a fantastic 'feature' that has caused me grief for the past hour or so.

Rich Linnell
  • 973
  • 5
  • 14
-1

By default your control id is your model binding value, You can also Just use firebug. select the control and get by default control id.

Mohammad Atiour Islam
  • 5,380
  • 3
  • 43
  • 48