Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Put this code somewhere:</p> <pre><code>using System; using System.Linq.Expressions; using System.Web.Mvc; namespace MvcLibrary.Extensions { public static class HtmlExtensions { public static MvcHtmlString FieldIdFor&lt;TModel, TValue&gt;(this HtmlHelper&lt;TModel&gt; html, Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression) { string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string inputFieldId = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName); return MvcHtmlString.Create(inputFieldId); } } } </code></pre> <p>Then in your ASPX view:</p> <pre><code>&lt;label for="&lt;%= Html.FieldIdFor(m =&gt; m.EmailAddress) %&gt;"&gt;E-mail address:&lt;/label&gt; &lt;%= Html.TextBoxFor(m =&gt; m.EmailAddress) %&gt; </code></pre> <p>You can also use this in JavaScript calls as you won't know the control's ID in advance and may need it for some JavaScript code to work against it:</p> <pre><code>&lt;script&gt; $.CoolJQueryFunction('&lt;%= Html.FieldIdFor(m =&gt; m.EmailAddress) %&gt;'); &lt;/script&gt; </code></pre> <p>The LabelFor HTML helper method, that someone mentioned here, won't let you specify the actual text label you want to use, you have to decorate your ViewModels with attributes to set the label text, with IMHO is ugly. I'd rather that stuff appear in the actual ASPX view part itself, not on some domain/view model. Some people will disagree with me though.</p> <p>Not sure of the rules for posting links to one's blog posts, but I posted a blog on this exact topic: <a href="http://www.dominicpettifer.co.uk/Blog/37/strongly-typed--label--elements-in-asp-net-mvc-2" rel="noreferrer">http://www.dominicpettifer.co.uk/Blog/37/strongly-typed--label--elements-in-asp-net-mvc-2</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload