razorhtml-helperid-generation

How can I know what id Razor Html helpers generate?


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.


Solution

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