0
我建立的出現在我的很多看法標準DropDownLists我自己的HtmlHelper擴展。在其他元素我用「EditorFor」和剃刀生成適當的元素「name」屬性,我自認爲是很重要的它被正確地綁定到模型。我怎麼會在我看來得到正確的名稱,以便我的助手適當命名的元素?Get生成的元素「名稱」屬性的HtmlHelper擴展
目前我的視圖代碼看起來像這樣,但我寧願不硬編碼的元素名稱,如果我能避免它。
<tr>
<td class="editor-label">
County:
</td>
<td class="editor-field">
@Html.CountyDropDown("CountyID")
</td>
</tr>
這裏是我的擴展代碼(返回縣根據當前用戶的區域列表):
<Extension()> _
Public Function CountyDropDown(ByVal html As HtmlHelper, ByVal name As String) As MvcHtmlString
Dim db As New charityContainer
Dim usvm As New UserSettingsViewModel
Dim ddl As IEnumerable(Of SelectListItem)
ddl = (From c In db.Counties Where c.RegionId = usvm.CurrentUserRegionID
Select New SelectListItem() With {.Text = c.Name, .Value = c.Id})
Return html.DropDownList(name, ddl)
End Function