15

Is there a way to get the client id generated for a field generated with a html helper?

i have several components that sometimes are inside another forms, and i wan't to attach javascript events to them.

so, for sample, i would like to have:

@Html.TextBoxFor(model => model.client.email)

<script> 
$('#@getmyusercontrolid(model=>model.client.email)').val("put your mail here");
</script>
Jokin
  • 4,188
  • 2
  • 31
  • 30

4 Answers4

31

I use this helper:

public static partial class HtmlExtensions
{
    public static MvcHtmlString ClientIdFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
    {
        return MvcHtmlString.Create(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression)));
    }
}

Use it just as you would any other helper: @Html.ClientIdFor(model=>model.client.email)

Mrchief
  • 75,126
  • 20
  • 142
  • 189
John Landheer
  • 3,999
  • 4
  • 29
  • 53
  • Sorry to mix my metaphors but this works like a well-oiled charm – DavidWainwright Nov 16 '12 at 13:09
  • Where are you getting `ExpressionHelper.GetExpressionText` from? – mxmissile May 21 '13 at 20:11
  • In order to use the helper on my page I had to import the namespace where the helper is located: @import MyWebProj.Helpers. Fore more information, see http://stackoverflow.com/questions/3239006/how-to-import-a-namespace-in-razor-view-page (it is also possible to use web.config inside the Views folder to handle the imports) – Juha Palomäki Jun 08 '13 at 11:43
  • 1
    You could have just used `html.IdFor(expression)` – Professor of programming Aug 28 '15 at 12:19
  • **Out of date as of MVC 4**, future readers—`IdFor` does this out of the box now; see [@Bonner웃's answer](https://stackoverflow.com/a/32270976/1958726) – Eric Eskildsen Jul 21 '17 at 23:09
11

MVC 5 has NameFor, so your example using it is:

@Html.TextBoxFor(model => model.client.email)

<script> 
$('#@Html.NameFor(model => model.client.email)').val("put your mail here");
</script>

EDIT

IdFor is also available, which may be more appropriate in this situation than NameFor.

9

Use @Html.IdFor(model => model.client.email) as this functionality already exists like so:

@Html.TextBoxFor(model => model.client.email)

<script> 
$('#@Html.IdFor(model => model.client.email)').val("put your mail here");
</script>

You can read more about it at the following page: https://msdn.microsoft.com/en-us/library/hh833709%28v=vs.118%29.aspx

Professor of programming
  • 2,978
  • 3
  • 30
  • 48
5

You could try specifying the id in the HtmlAttributes argument when you generate the input field, e.g.

@Html.TextBoxFor(model => model.client.email, new { id = "emailTextBox" })

Then you can use this in your javaScript

var email = $('#emailTextBox').val();
tobias86
  • 4,979
  • 1
  • 21
  • 30
  • that will not work if you have more than one instances of the component in the same page, and its also not useful if you want to databind in the destination action. – Jokin Mar 24 '11 at 15:04
  • I know this is an old answer but... when using MVC and Razor view engine you need to escape the id using the `@` symbol example: `@id="emailTextBox"` – Chef_Code Mar 16 '16 at 04:56