自定义的HtmlHelper扩展方法在View不可用? [英] Custom HtmlHelper extension method not available in View?
本文介绍了自定义的HtmlHelper扩展方法在View不可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有翻译耶利米·克拉克的的CheckBoxList帮手MVC 的到我的VB.Net项目,但是当我尝试使用方法在我看来,我得到的错误
'的CheckBoxList不是System.Web.Mvc.HtmlHelper(中Attenda.Stargate.Web.UserRolesViewModel)中的一员。
谁能告诉我哪里出了问题?
助手模块:
导入System.Runtime.CompilerServices公用模块InputExtensions <扩展()> _
公共职能的CheckBoxList(BYVAL作为的HtmlHelper的HtmlHelper,BYVAL名作为字符串,BYVAL列表信息作为列表(作者列表项))作为字符串
返回htmlHelper.CheckBoxList(名称,列表信息,DirectCast(没什么,IDictionary的(字符串,对象)))
结束功能 <扩展()> _
公共职能的CheckBoxList(BYVAL作为的HtmlHelper的HtmlHelper,BYVAL名作为字符串,BYVAL列表信息作为列表(中列表项),BYVAL htmlAttributes作为对象)作为字符串
返回htmlHelper.CheckBoxList(名称,列表信息,DirectCast(新RouteValueDictionary(htmlAttributes),IDictionary的(字符串,对象)))
结束功能 <扩展()> _
公共职能的CheckBoxList(BYVAL作为的HtmlHelper的HtmlHelper,BYVAL名作为字符串,BYVAL列表信息作为列表(中列表项),BYVAL htmlAttributes作为IDictionary的(字符串,对象))作为字符串
如果String.IsNullOrEmpty(名称),然后
抛出新ArgumentException的(参数必须有一个值,名称)
万一
如果列表信息是没有那么
抛出新的ArgumentNullException(列表信息)
万一
如果listInfo.Count< 1然后
抛出新的ArgumentException(以下简称名单必须至少包含一个值,列表信息)
万一
昏暗某人作为新的StringBuilder()
对于每一个信息作为列表项在列表信息
昏暗的建设者作为新TagBuilder(输入)
如果info.Selected然后
builder.MergeAttribute(选中,选中)
万一
builder.MergeAttributes(字符串,对象)(htmlAttributes)
builder.MergeAttribute(类型,复选框)
builder.MergeAttribute(值,info.Value)
builder.MergeAttribute(名,名称)
builder.InnerHtml = info.Text
sb.Append(builder.ToString(TagRenderMode.Normal))
sb.Append(< BR />中)
下一个
返回sb.ToString()
结束功能前端模块
查看源代码:
<%@页标题=LANGUAGE =VB的MasterPageFile =〜/查看/共享/ TwoColumn.Master继承=System.Web.Mvc.ViewPage(的Attenda.Stargate.Web.UserRolesViewModel)%GT;< ASP:内容ID =内容1ContentPlaceHolderID =TitleContent=服务器>
编辑用户角色
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
< H2>编辑的角色
<%= Html.En code(Model.User.UserName)%GT;< / H>
< DIV>
<%= Html.CheckBoxList(角色,Model.Roles)%GT;
< / DIV>
< / ASP:内容>
< ASP:内容ID =Content3ContentPlaceHolderID =cphLeftPanel=服务器>
< / ASP:内容>
解决方案
您需要将包含自定义助手类的名称空间导入到您的视图页面。您可以在页面本身或所有页面的web.config文件做到这一点。把code到一个命名空间第一。
<%@导入命名空间=MyProject.Extensions%GT;
或(在web.config)
<网页和GT;
...
<&命名空间GT;
...
<添加命名空间=MyProject.Extensions/>
< /命名空间>
< /页>
I have translated Jeremiah Clark's CheckBoxList Helper for MVC into my VB.Net project but when I try to use the method in my view I get the error
'CheckBoxList' is not a member of 'System.Web.Mvc.HtmlHelper(Of Attenda.Stargate.Web.UserRolesViewModel)'.
Can anyone tell me where I have gone wrong?
Helper module:
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
View source:
<%@ 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>
解决方案
You need to import the namespace containing the custom helper class into your view page. You can do this on the page itself or in the web.config file for all pages. Put the code into a namespace first.
<%@ Import Namespace="MyProject.Extensions" %>
or (in the web.config)
<pages>
...
<namespaces>
...
<add namespace="MyProject.Extensions" />
</namespaces>
</pages>
这篇关于自定义的HtmlHelper扩展方法在View不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文