views:

471

answers:

1

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?

+3  A: 

I think you can do something like:

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

Overloads of TextBoxFor

bruno conde
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 <input id="Websites_0__Url" ...> etc.
Colin Asquith
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