1

In my view I want to be able to render out the ID of a model property independent of an associated control markup.

So for example, if my model was:

public class Author {
    public string Title { get; set; }
    public Address Address { get; set; }
}

In my view I would preferably like to be able to use an extension of HtmlHelper to render out the Title property's ID using a lambda. Something like:

@Html.GetClientIdFor(x => x.Title)

And this would output "Title".

In the case of nested objects, I would like:

@Html.GetClientIdFor(x => x.Address.PostCode)

to output "Address_PostCode".

I'm sure I can access the data from the ViewData.TemplateInfo object but from there I'm a little stuck.

Any ideas? I am using MVC4 but I would like a solution that worked for MVC3 too.

Digbyswift
  • 10,310
  • 4
  • 38
  • 66

1 Answers1

3

Unless I'm misunderstanding your question, there is an HtmlHelper that does exactly what you want: IdFor

@Html.IdFor(x => x.Address.PostCode) // Will return Address_PostCode

Although, that's for MVC4 and up. For MVC3, you will need to roll your own. See this SO.

Community
  • 1
  • 1
Simon Belanger
  • 14,752
  • 3
  • 41
  • 35