MVC3+ Auto-adds element ids!

Wow, so I just found this out today, and figured I'd share it with the world before I forgot again ;-).

If you're editing a Razor-view and you're asking for an element's ID:

Dim ddlFieldId = ViewData.TemplateInfo.GetFullHtmlFieldId("countryNumber")

... then just render an element with the same name:

Html.DropDownList("countryNumber", selectList, "pick one")

... the element will automatically get the ID you requested earlier! Previously, I've gone about it this way:

Html.DropDownList("countryNumber", selectList, "pick one", New With {.id = ddlFieldId})

... but now, it's easier. Isn't that just awesome? :-D

Have a great day!

// S

Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints