简体   繁体   中英

Upload with Strongly Typed View - Problem

I'm have a problem with Upload with strongly typed view in ASP.NET MVC. The value of input (view) is coming null for the view. My code follow:

<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Create", "DownloadsSub", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {%>
    <%: Html.ValidationSummary(true)%>

    <fieldset>
        <legend>Informações do download</legend>

        <div class="editor-label">
            Selecione a categoria do download
        </div>
        <div class="editor-field">
            <%: Html.DropDownList("IDCategoriaDownloads", (IEnumerable<SelectListItem>)ViewData["IDCategoriaDownloads"], "Categorias de downloads...")%>
            <%: Html.ValidationMessageFor(model => model.IDCategoriaDownloads)%>
        </div>

        <div class="editor-label">
            Selecione o formato do arquivo
        </div>
        <div class="editor-field">
            <%= Html.DropDownList("IDFormatoArquivo", (IEnumerable<SelectListItem>)ViewData["IDFormatoArquivo"], "Formatos de arquivos...") %>
            <%: Html.ValidationMessageFor(model => model.IDFormatoArquivo)%>
        </div>

        <div class="editor-label">
            Título do download
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.TituloDownload)%>
            <%: Html.ValidationMessageFor(model => model.TituloDownload)%>
        </div>

        <div class="editor-label">
            Descrição do download
        </div>
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.DescricaoDownload)%>
            <%: Html.ValidationMessageFor(model => model.DescricaoDownload)%>
        </div>

        <div class="editor-label">
            Data de Postagem
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.DataDownload)%>
            <%: Html.ValidationMessageFor(model => model.DataDownload)%>
        </div>

        <div class="editor-label">
            Selecione o arquivo para download
        </div>
        <div class="editor-field">
            <input type="file" id="txtFile" name="txtFile" />
            <%--<%: Html.TextBoxFor(model => model.CaminhoDownload) %>
            <%: Html.ValidationMessageFor(model => model.CaminhoDownload)%>--%>
        </div>

        <div class="editor-label">
            Quantidade inicial de cliques
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.HitsDownload)%>
            <%: Html.ValidationMessageFor(model => model.HitsDownload)%>
        </div>

        <div class="editor-label">
            Qual o tamanho do arquivo em bytes?
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.TamanhoDownload) %>
            <%: Html.ValidationMessageFor(model => model.TamanhoDownload) %>
        </div>

        <div class="editor-label">
            Possui direitos autorais?
        </div>
        <div class="editor-field">
            <%= Html.DropDownList("StatusDireitosAutorais", (IEnumerable<SelectListItem>)ViewData["StatusDireitosAutorais"], "Selecione...")%>
            <%: Html.ValidationMessageFor(model => model.StatusDireitosAutorais)%>
        </div>

        <div class="editor-label">
            Direitos autorais (preencha apenas se houver)
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.CreditosDownload)%>
            <%: Html.ValidationMessageFor(model => model.CreditosDownload)%>
        </div>

        <p>
            <input type="submit" value="Salvar" />
        </p>
    </fieldset>

<% } %>

And the controller

[HttpPost]
    public ActionResult Create(tbDownloads _novoDownload, HttpPostedFileBase arquivoUp)
    {
        //var arquivoUp = this.Request.Files[0];
        string nomeArquivoSalvo = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FilesUpload", "Downloads");
        nomeArquivoSalvo = Path.Combine(nomeArquivoSalvo, Path.GetFileName(arquivoUp.FileName));
        arquivoUp.SaveAs(Server.MapPath("~/FilesUpload/Downloads/") + nomeArquivoSalvo);
        _novoDownload.CaminhoDownload = nomeArquivoSalvo.ToString();

        if (ModelState.IsValid)
        {
            modelo.AddTotbDownloads(_novoDownload);
            modelo.SaveChanges();

            return RedirectToAction("Sucesso", "Mensagens");
        }

        return View(_novoDownload);
    }

Ideas?

Thanks for help!

The name of the HttpPostedFileBase property needs to match the name of the input field in the view.

In the view, you have called it "txtFile":

<div class="editor-field">
  <input type="file" id="txtFile" name="txtFile" />
  <%--<%: Html.TextBoxFor(model => model.CaminhoDownload) %>
  <%: Html.ValidationMessageFor(model => model.CaminhoDownload)%>--%>
</div>

But in the controller you refer to it as:

[HttpPost]
public ActionResult Create(tbDownloads _novoDownload,
                           HttpPostedFileBase arquivoUp)
{ [...] }

If you change this to:

[HttpPost]
public ActionResult Create(tbDownloads _novoDownload,
                           HttpPostedFileBase txtFile)
{ [...] }

It should all work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM