2

In a Razor view, assume I have something like this:

@Html.HiddenFor(model => model.Värde)

Here, "Värde" is Swedish for "Value". Note it contains "ä". For this, Razor will generate HTML like this:

<input id="V_rde" name="Värde" type="hidden" />

Note that the element's id is "V_rde", instead of "Värde".

So, how do I access this element from JavaScript using getElementById? I understand that I can use 'V_rde', but I want something that will work generically, not just for "Värde", something like this (meaning htmlAttributes parameter):

@Html.Whatever(..., new { onclick = "JavaScript:alert(document.getElementById('@Html.IdFor("Värde")'));" })

Where I would like @Html.IdFor(string name) to convert the name exactly like the id is converted in the @Html helpers for labels, checkboxes, editboxes etc.

Charles
  • 50,943
  • 13
  • 104
  • 142
Kjell Rilbe
  • 1,331
  • 14
  • 39

2 Answers2

4

Meanwhile @Html.IdFor(x=>x.YourProperty) made it into MVC, I think this is the easy solution now

pb2q
  • 58,613
  • 19
  • 146
  • 147
guido
  • 381
  • 7
  • 12
1

Does this work?

@HtmlHelper.GenerateIdFromName("originalName")

If so you could easily make your own HtmlHelper extension which takes a lambda expression.

pb2q
  • 58,613
  • 19
  • 146
  • 147
Peter Morris
  • 20,174
  • 9
  • 81
  • 146