How to get EditorFor ID with nested view modes in asp.net mvc 2

So, I have two nested view models, CreditCard → BillAddress. I have a view of "EditBilling" that has EditorFor (CreditCard). The CreditCard EditorTemplate has an editor (BillAddress), and the BillAddress EditorTemplate has an editor for (BillState).

The end result is a select list with the identifier "CreditCard_BillAddress_BillState".

I need to reference this in javascript, so you need to know the id. In other situations with non-nested ViewModels, I used the following code:

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

The problem is that the ModelMetadata.PropertyName property is known only about the current property and not the parent (s). So I get the following:

$('#BillAddress_BillState')

How can I get the client identifier of nested strongly typed helpers? Thanks in advance.

+3
source share
1 answer

I believe that I found a solution by looking at the source code of TemplateHelpers. It seems that ViewData.TemplateInfo.HtmlFieldPrefix gives the full “name” (mostly with “.” As a delimiter instead of “_”).

+5
source

Source: https://habr.com/ru/post/1739193/


All Articles