ビューでは使用できませんカスタムのHtmlHelperの拡張メソッド?
-
12-09-2019 - |
質問
私は、エレミヤクラークの翻訳していますCheckBoxListのヘルパーは、MVCのための私のVB.Netのプロジェクトにのが、私は私の見解でメソッドを使用しようとすると、私はエラーを取得します。
'CheckBoxList' is not a member of 'System.Web.Mvc.HtmlHelper(Of Attenda.Stargate.Web.UserRolesViewModel)'.
私が間違っているところ誰も私を伝えることができますか?
ヘルパーモジュール:
Imports System.Runtime.CompilerServices
Public Module InputExtensions
<Extension()> _
Public Function CheckBoxList(ByVal htmlHelper As HtmlHelper, ByVal name As String, ByVal listInfo As List(Of ListItem)) As String
Return htmlHelper.CheckBoxList(name, listInfo, DirectCast(Nothing, IDictionary(Of String, Object)))
End Function
<Extension()> _
Public Function CheckBoxList(ByVal htmlHelper As HtmlHelper, ByVal name As String, ByVal listInfo As List(Of ListItem), ByVal htmlAttributes As Object) As String
Return htmlHelper.CheckBoxList(name, listInfo, DirectCast(New RouteValueDictionary(htmlAttributes), IDictionary(Of String, Object)))
End Function
<Extension()> _
Public Function CheckBoxList(ByVal htmlHelper As HtmlHelper, ByVal name As String, ByVal listInfo As List(Of ListItem), ByVal htmlAttributes As IDictionary(Of String, Object)) As String
If String.IsNullOrEmpty(name) Then
Throw New ArgumentException("The argument must have a value", "name")
End If
If listInfo Is Nothing Then
Throw New ArgumentNullException("listInfo")
End If
If listInfo.Count < 1 Then
Throw New ArgumentException("The list must contain at least one value", "listInfo")
End If
Dim sb As New StringBuilder()
For Each info As ListItem In listInfo
Dim builder As New TagBuilder("input")
If info.Selected Then
builder.MergeAttribute("checked", "checked")
End If
builder.MergeAttributes(Of String, Object)(htmlAttributes)
builder.MergeAttribute("type", "checkbox")
builder.MergeAttribute("value", info.Value)
builder.MergeAttribute("name", name)
builder.InnerHtml = info.Text
sb.Append(builder.ToString(TagRenderMode.Normal))
sb.Append("<br />")
Next
Return sb.ToString()
End Function
End Module
ソースを表示:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/TwoColumn.Master" Inherits="System.Web.Mvc.ViewPage(Of Attenda.Stargate.Web.UserRolesViewModel)" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit User Roles
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit Roles for
<%=Html.Encode(Model.User.UserName)%></h2>
<div>
<%=Html.CheckBoxList("Roles", Model.Roles)%>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cphLeftPanel" runat="server">
</asp:Content>
解決
あなたはあなたのビューページへのカスタムヘルパークラスを含む名前空間をインポートする必要があります。あなたは、ページ自体にまたはすべてのページのweb.configファイルでこれを行うことができます。最初の名前空間にコードを入れます。
<%@ Import Namespace="MyProject.Extensions" %>
または(web.configファイルで)
<pages>
...
<namespaces>
...
<add namespace="MyProject.Extensions" />
</namespaces>
</pages>
他のヒント
私は公開するモジュールを宣言していなかったので、
私はこの問題が発生します。
この質問はまたのここではを尋ねましたP>
所属していません StackOverflow