Image Management MVC VB.NET

0

Good, I need a helper that allows me to preview an image that I want to upload, the problem is that it does not show me the preview. I show you my code:

This is my helper

    Imports System.Runtime.CompilerServices
    Imports System.Web.Mvc
    Imports System.Linq.Expressions
    Namespace Helpers


Public Module ImageHelpers
    Sub New()
    End Sub
    <System.Runtime.CompilerServices.Extension>
    Public Function UploadImageFor(Of TModel, TProperty)(helper As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TProperty)), Optional id As String = Nothing, Optional name As String = Nothing, Optional cssclass As String = Nothing, Optional alt As String = Nothing,
    Optional imgId As String = Nothing, Optional height As String = Nothing, Optional width As String = Nothing) As MvcHtmlString
        Const type As String = "file"

        If cssclass Is Nothing Then
            cssclass = "upload"
        End If

        If id Is Nothing Then
            id = "File1"
        End If

        If name Is Nothing Then
            name = "imageLoad2"
        End If

        If alt Is Nothing Then
            alt = "Preview"
        End If

        If imgId Is Nothing Then
            imgId = "imgThumbnail2"
        End If

        If height Is Nothing Then
            height = "126px"
        End If

        If width Is Nothing Then
            width = "126px"
        End If

        Dim fileBuilder = New TagBuilder("input")
        Dim imgBuilder = New TagBuilder("img")
        Dim mergedBuilder = New TagBuilder("p")

        imgBuilder.MergeAttribute("width", width)

        mergedBuilder.AddCssClass("file-upload-")
        mergedBuilder.InnerHtml += fileBuilder.ToString()
        mergedBuilder.InnerHtml += imgBuilder.ToString()
        Return MvcHtmlString.Create(mergedBuilder.ToString())

    End Function

End Module

End Namespace

specifically in

        mergedBuilder.InnerHtml += fileBuilder.ToString()
        mergedBuilder.InnerHtml += imgBuilder.ToString()

add .ToString () because I had an error with the "+" operator (the code was passed from Csharp to vb.net).

So I have defined my model

<Display(Name:="Ingrese Foto de Perfil")>
<Required(ErrorMessage:="Debe ingresar la foto de perfil")>
Property FotoPerfil As Image

and you should be able to use the Helper from a view like this:

@Html.LabelFor(Function(m) m.FotoPerfil)
@Html.UploadImageFor(Function(m) m.FotoPerfil)

also this is the original code in Csharp:

public static class ImageHelpers {
public static MvcHtmlString UploadImageFor<TModel, TProperty>
(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression, string id = null,
string name = null, string cssclass = null, string alt = null,
string imgId = null, string height = null, string width = null)
{
const string type = "file";

     if (cssclass == null)
     {
         cssclass = "upload";
     }

     if (id == null)
     {
         id = "File1";
     }

     if (name == null)
     {
         name = "imageLoad2";
     }

     if (alt == null)
     {
         alt = "Preview";
     }

     if (imgId == null)
     {
         imgId = "imgThumbnail2";
     }

     if (height == null)
     {
         height = "126px";
     }

     if (width == null)
     {
         width = "126px";
     }

     var fileBuilder = new TagBuilder("input");
     var imgBuilder = new TagBuilder("img");
     var mergedBuilder = new TagBuilder("p");

     ///Firstly we build the input tag. //--Add the CSS class. fileBuilder.AddCssClass(cssclass);        
    //--Add the name. fileBuilder.MergeAttribute("name", name);        
    //--Add the id. fileBuilder.MergeAttribute("id",id);        
    //--Add the type. fileBuilder.MergeAttribute("type",type);

    ///Secondly we build the img tag. //--Add the alt. imgBuilder.MergeAttribute("alt", alt);        
    //--Add the id. imgBuilder.MergeAttribute("id", imgId);        
    //--Add the height and width. imgBuilder.MergeAttribute("height", height);        
    imgBuilder.MergeAttribute("width", width);

     ///Merge the two together into a p tag. 
    mergedBuilder.AddCssClass("file-upload-");        
    mergedBuilder.InnerHtml += fileBuilder;        
    mergedBuilder.InnerHtml += imgBuilder;        
    return MvcHtmlString.Create(mergedBuilder.ToString());

 }

}
    
asked by ASP.NEET 21.11.2016 в 19:27
source

0 answers