gpt4 book ai didi

asp.net-mvc - 自定义 HtmlHelper 扩展方法在 View 中不可用?

转载 作者:行者123 更新时间:2023-12-03 18:10:20 25 4
gpt4 key购买 nike

我翻译了 Jeremiah Clark 的 CheckBoxList Helper for MVC进入我的 VB.Net 项目,但是当我尝试在我的 View 中使用该方法时出现错误

'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>

最佳答案

您需要将包含自定义帮助程序类的命名空间导入您的 View 页面。您可以在页面本身或在所有页面的 web.config 文件中执行此操作。首先将代码放入命名空间。

<%@ Import Namespace="MyProject.Extensions" %>

或(在 web.config 中)
<pages>
...
<namespaces>
...
<add namespace="MyProject.Extensions" />
</namespaces>
</pages>

关于asp.net-mvc - 自定义 HtmlHelper 扩展方法在 View 中不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1105276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com