Hide form after uploading a file

Asked

Viewed 21 times

0

Hello, I have a view that has more than one form and in each form, I specify the type of document that will be uploaded. ex: Form1 has an Hidden input with value = 1, for Cpf... form2 value = 2, for rg... etc...
So when I give a post to server I take this value and save the file converted to Base64 in the database with the name of the document type: in case 1, as Cpf. All I want, and I don’t know how to do... is when the post is finished and when you return the Formulars view, Form1 is hidden.

My View

@model BancoBBB.Models.Anexo

@{
    ViewBag.Title = "Anexar";
    Layout = "~/Views/_Layout.cshtml";
}

<h2>Anexar</h2>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })


<div class="col col-md-12 col-sm-12">
    
    <form action="/Anexo/Upload" class="form-horizontal form-label-left" enctype="multipart/form-data" id="frmCpf" method="post" name="frmCpf">
        <div class="row">
            <div class="col-6">
                <label>CPF</label>
                <input type="hidden" value="1" name="TipoDocumento" id="TipoDocumento" />
                <input id="File" type="file" name="File" class="form-control" />
            </div>

            <div class="col-3" style="padding-top:25px;">

                <input type="submit" value="Enviar" class="btn btn-lg btn-success" />

            </div>
        </div>
    </form>
    

    @using (Html.BeginForm("Upload", "Anexo", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal form-label-left", name = "frmRg" }))
    {

        <div class="row">
            <div class="col-6">
                <label>RG</label>
                <input type="hidden" value="2" name="TipoDocumento" id="TipoDocumento" />
                <input id="File" type="file" name="File" class="form-control" />
            </div>
            <div class="col-3" style="padding-top:25px;">
                <input type="submit" value="Enviar" class="btn btn-lg btn-success" />
            </div>
        </div>
    }

    @using (Html.BeginForm("Upload", "Anexo", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal form-label-left", name = "frmRes" }))
    {

        <div class="row">
            <div class="col-6">
                <label>Comprovante de Residência</label>
                <input type="hidden" value="3" name="TipoDocumento" id="TipoDocumento" />
                <input id="File" type="file" name="File" class="form-control" />
            </div>
            <div class="col-3" style="padding-top:25px;">
                <input type="submit" value="Enviar" class="btn btn-lg btn-success" />
            </div>
        </div>
    }
</div>

<div class="row">
    @Html.ActionLink("Back to List", "Index")
</div>

My Model

    {
        public int AnexoId { get; set; }
        public Documento TipoDocumento { get; set; }// Classe enum
        public string Arquivo { get; set; }//Propriedade que vai receber a conversão para base64
        public string Formato { get; set; }
        [NotMapped]
        public HttpPostedFileBase File { get; set; }
    } 

    public enum Documento
    {
        Cpf = 1,
        Rg = 2,
        ComprovanteResidencia = 3,
    }

Annex controller

public ActionResult Anexar()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(Anexo anexo)
        {
            if (ModelState.IsValid)
            {
                anexo.Arquivo = ConverterArquivoBase64(anexo.File);
                anexo.Formato = anexo.File.ContentType;
                anexoDao.Salvar(anexo);
                return RedirectToAction("Anexar");
            }
            return View("Anexar");
        }

        public static string ConverterArquivoBase64(HttpPostedFileBase file)
        {
            byte[] arquivo = new byte[pdf.ContentLength];
            file.InputStream.Read(arquivo, 0, file.ContentLength);
            return Convert.ToBase64String(arquivo);
        }

1 answer

0

Your application is a bit confused, I would not have two Forms in the same view just to separate the document type, but one way to resolve this your situation is always pass a template to your view, and in return you indicate to the model who you want to hide. in case the first time, the property would be void, and you would not hide.

ex. in cshtm @model Meumodel

in the controller

Return View("Attach", Mymodel);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.