2010-11-08 44 views
0

我在ASP.NET MVC中使用强类型视图上传时遇到问题。 输入(视图)的值对视图为空。我的代码如下:以强类型查看上传 - 问题

<% 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> 

<% } %> 

而且控制器

[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); 
    } 

想法?

感谢您的帮助!

回答

1

HttpPostedFileBase属性的名称需要与视图中输入字段的名称匹配。

在视图中,你可以把它叫做 “txtFile”:

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

但在控制器,你把它称为:

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

如果将其更改为:

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

它应该都可以工作。