【发布时间】:2011-05-23 18:20:41
【问题描述】:
我正在为出现在我的许多视图中的标准 DropDownLists 构建自己的 HtmlHelper 扩展。在其他元素上,我使用“EditorFor”,razor 为我生成正确的元素“name”属性,因为这对于正确绑定到模型很重要。如何在我的视图中获得正确的名称,以便我的助手正确地命名元素?
目前我的视图代码看起来像这样,但如果可以避免的话,我宁愿不硬编码元素名称。
<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
【问题讨论】:
标签: asp.net-mvc-3 razor html-helper