How to get ID of EditorFor with nested viewmodels in asp.net mvc 2

Posted by Luke on Stack Overflow See other posts from Stack Overflow or by Luke
Published on 2010-03-30T19:15:58Z Indexed on 2010/04/03 7:13 UTC
Read the original article Hit count: 325

Filed under:
|

So I have two nested view models, CreditCard -> BillAddress. I have a view, "EditBilling", that has EditorFor(CreditCard). The CreditCard EditorTemplate has EditorFor(BillAddress), and the BillAddress EditorTemplate has EditorFor(BillState).

The end result is a select list with id "CreditCard_BillAddress_BillState".

I need to reference this in javascript, thus need to know the ID. In other situations, with non-nested ViewModels, I have used the following code:

$('#<%= ViewData.ModelMetadata.PropertyName %>_BillState')

The problem here is that the ModelMetadata.PropertyName property is only aware of the current property, not the parent(s). So I end up with the following:

$('#BillAddress_BillState')

How does one go about getting the client ID of nested strongly typed helpers? Thanks in advance.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc-2