Reference to the control identifier created using TextBoxFor ()

I love ASP.NET MVC, keeping up with releases / documents is sometimes difficult, so maybe I just don’t get something ... I want to use TextBoxFor () and work with LabelFor (), etc., that's all magic happens to me.

But if I create ...

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

And I wanted to do something nice with jQuery, how would I get the identifier of the created control? I could add a CSS class and use it to attach jQuery, but for something I do, I need an identifier ... so I could do something like:

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

I know that in this case I can figure it out and crack it manually, but is there a more accurate way?

+7
asp.net-mvc-2
source share
4 answers

Since MVC4 has a built-in way to do this, @ Html.IdFor () .

Here is an example of its use:

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

and the result is similar to

 Filters_Occurred_From 
+7
source share

I think you can do something like:

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

TextBoxFor overloads

+12
source share

It seems that the Html.Textbox () code will generate an identifier, duplicating the name of the control for everything that starts with the letter (az). If, however, your “name” begins with a number that just doesn't bother you.

This is a fantastic “feature” that has caused me grief in the last hour or so.

0
source share

By default, your control ID is your model binding value. You can also use Firebug. select the control and get the default identifier of the control.

-one
source share

All Articles