webgrid里面的htmlhelper

时间:2011-03-01 22:29:29

标签: asp.net-mvc-3 webgrid

我正在使用MVC3,我需要一个带图像的HTMLHelper,所以我找到了一个很好的代码,问题是图像没有显示而不是我看到html代码ex:

<a href='whatever'><img src="images/printer.png" /></a>

也许这是因为帮助器在webgrid助手里面,我真的不知道,有什么建议吗?

辅助

<Extension()> _
    Public Function ActionLinkWithImage(ByVal helper As HtmlHelper, ByVal actionName As String, ByVal controllerName As String, ByVal imageUrl As String, ByVal alternateText As String, ByVal routeValues As Object, ByVal linkHtmlAttributes As Object, ByVal imageHtmlAttributes As Object) As String
        Dim urlHelper = New UrlHelper(helper.ViewContext.RequestContext)
        Dim url = urlHelper.Action(actionName, controllerName, routeValues)

        ' Create link
        Dim linkTagBuilder = New TagBuilder("a")
        linkTagBuilder.MergeAttribute("href", url)
        linkTagBuilder.MergeAttributes(New RouteValueDictionary(linkHtmlAttributes))

        ' Create image
        Dim imageTagBuilder = New TagBuilder("img")
        imageTagBuilder.MergeAttribute("src", urlHelper.Content(imageUrl))
        imageTagBuilder.MergeAttribute("alt", urlHelper.Encode(alternateText))
        imageTagBuilder.MergeAttributes(New RouteValueDictionary(imageHtmlAttributes))

        ' Add image to link
        linkTagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing)

        Return linkTagBuilder.ToString()
    End Function

WebGrid加载

@<div id="divGrid">
        @grid.GetHtml(
            tableStyle:="webgrid",
            headerStyle:="webgrid-header",
            footerStyle:="webgrid-footer",
            alternatingRowStyle:="webgrid-alternating-row",
            selectedRowStyle:="webgrid-selected-row",
            rowStyle:="webgrid-row-style",
            mode:=WebGridPagerModes.All,
            firstText:="<< Inicio",
            previousText:="< Anterior",
            nextText:="Siguiente >",
            lastText:="Fin >>",
            columns:=grid.Columns(
            grid.Column(format:=Function(Model) Html.ActionLink(" ", "Details", "CertificadoVehiculo", New With {.area = "Certificados", .parIDCertificado = Model.ID_CERTIFICADO, .parIDPoliza = Model.ID_POLIZA}, New With {.class = "imgConsultar", .title = "Consultar"}), style:="webgrid-width-images"),
            grid.Column(format:=Function(Model) Html.ActionLink(" ", "Edit", "CertificadoVehiculo", New With {.area = "Certificados", .parIDCertificado = Model.ID_CERTIFICADO, .parIDPoliza = Model.ID_POLIZA}, New With {.class = "imgEditar", .title = "Modificar"}), style:="webgrid-width-images"),
            grid.Column(format:=Function(Model) Html.ActionLinkWithImage("Action", "Controller", Url.Content("~/Images/printer.png"), "TextoAlternativo", Nothing, Nothing, Nothing), style:="webgrid-width-images"),
            grid.Column("ID_CERTIFICADO", "No. Certificado"),
            grid.Column("POLIZAS.NUMERO_POLIZA", "No. Poliza"),
            grid.Column("PRIMER_APELLIDO_ASEGURADO", "Asegurado", format:=Function(Model) Model.ASEGURADOS.PRIMER_APELLIDO_ASEGURADO & " " & Model.ASEGURADOS.SEGUNDO_APELLIDO_ASEGURADO & " " & Model.ASEGURADOS.NOMBRES_ASEGURADO),
            grid.Column("PRIMA_CERTIFICADO", "Prima Total"),
        grid.Column("ESTADOS.DESC_ESTADO", "Estado")
            )
        )
    </div>

1 个答案:

答案 0 :(得分:3)

ActionLinkWithImage助手返回MvcHtmlString而不是string。像这样:

<Extension()> _
Public Function ActionLinkWithImage(ByVal helper As HtmlHelper, ByVal actionName As String, ByVal controllerName As String, ByVal imageUrl As String, ByVal alternateText As String, ByVal routeValues As Object, ByVal linkHtmlAttributes As Object, ByVal imageHtmlAttributes As Object) As MvcHtmlString
    Dim urlHelper = New UrlHelper(helper.ViewContext.RequestContext)
    Dim url = urlHelper.Action(actionName, controllerName, routeValues)

    ' Create link
    Dim linkTagBuilder = New TagBuilder("a")
    linkTagBuilder.MergeAttribute("href", url)
    linkTagBuilder.MergeAttributes(New RouteValueDictionary(linkHtmlAttributes))

    ' Create image
    Dim imageTagBuilder = New TagBuilder("img")
    imageTagBuilder.MergeAttribute("src", urlHelper.Content(imageUrl))
    imageTagBuilder.MergeAttribute("alt", urlHelper.Encode(alternateText))
    imageTagBuilder.MergeAttributes(New RouteValueDictionary(imageHtmlAttributes))

    ' Add image to link
    linkTagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing)

    Return MvcHtmlString.Create(linkTagBuilder.ToString())
End Function