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.

2 Answers 2

4

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

1

Does this work?

@HtmlHelper.GenerateIdFromName("originalName")

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.