Buenas , necesito un helper que me permita pre visualizar una imagen que deseo subir, el problema es que no me muestra la pre visualización . Les muestro mi código :
Este es mi 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
especificamente en
mergedBuilder.InnerHtml += fileBuilder.ToString()
mergedBuilder.InnerHtml += imgBuilder.ToString()
agregue .ToString() porque tenia un error con el operador "+".(el código lo he pasado de Csharp a vb.net).
asi tengo definido mi modelo
<Display(Name:="Ingrese Foto de Perfil")>
<Required(ErrorMessage:="Debe ingresar la foto de perfil")>
Property FotoPerfil As Image
y debería poder usar el Helper desde una vista así:
@Html.LabelFor(Function(m) m.FotoPerfil)
@Html.UploadImageFor(Function(m) m.FotoPerfil)
también este es el codigo original en 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());
}
}