2011-07-27 16 views
3

也有類似的帖子,但他們似乎並不代表我的情況。如果這是重新發布,請提前道歉。ASP.NET MVC3從同一頁發送多個表格

我有我的看法是

@FileUpload.GetHtml(initialNumberOfFiles:1,allowMoreFilesToBeAdded:true,includeFormTag:true, uploadText: "Upload") 

@model IEnumerable<EpubsLibrary.Models.Partner> 
@{ using (Html.BeginForm("Index","Epub")) 
    { 
     @Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None") 
     <input type="submit" value="send" id="pickPartner" hidden="hidden"/> 
    }   
} 

<script type="text/javascript"> 
    $(".file-upload-buttons input").click(function() { 
     $("#pickPartner").click(); 
    }); 
</script> 

我控制器

[HttpPost] 
public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, FormCollection collection) 
{ 
    int selectedPartner, count =0; 
    //int selectedPartner = int.Parse(collection["PartnerID"]); 
    if(!int.TryParse(collection["PartnerID"], out selectedPartner)) 
    { 
     selectedPartner = 0; 
     ModelState.AddModelError("", "You must pick a publishing agency"); 
    } 
    IList<Partner> p = r.ListPartners(); 
    ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name", selectedPartner); 

    //make sure files were selected for upload 
    if (fileUpload != null) 
    { 
     for (int i = 0; i < fileUpload.Count(); i++) 
     { 
      //make sure every file selected != null 
      if (fileUpload.ElementAt(i) != null) 
      { 
       count++; 
       var file = fileUpload.ElementAt(i); 
       if (file.ContentLength > 0) 
       { 
        var fileName = Path.GetFileName(file.FileName); 
        // need to modify this for saving the files to the server 
        var path = Path.Combine(Server.MapPath("/App_Data/uploads"), Guid.NewGuid() + "-" + fileName); 
        file.SaveAs(path); 
       } 
      } 
     } 
    } 

    if (count == 0) 
    { 
     ModelState.AddModelError("", "You must upload at least one file!"); 
    } 
    return View(); 
} 

我使用微軟的Web助手文件上傳助手上傳文件。我遇到的問題是助手創建了一個表單,並且我還需要在同一頁面上提交另一個表單以提交數據。

我想我可以鏈接提交按鈕,這樣當你點擊上傳時,它也發送了其他表單數據但數據沒有被髮送。每個表單都獨立於另一個而沒有問題,但我需要他們一起工作。任何意見,將不勝感激。

好的,我更新了

@model IEnumerable<EpubsLibrary.Models.Partner> 
@{ using (Html.BeginForm("Index","Epub")) 
    { 
     @Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None") 
     @FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload") 
     <input type="submit" value="send" id="pickPartner"/> 
    }   
} 

的看法,但現在該文件中的數據似乎並沒有被得到了通過。

- 更新 -

我做了以下更改。 視圖現在看起來像

@model IEnumerable<EpubsLibrary.Models.Partner> 
@{ using (Html.BeginForm("Index", "Epub", new { enctype = "multipart/form-data" })) 
    { 
     @Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None") 
     @FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload") 
     <input type="submit" value="send" id="pickPartner"/> 
    }   
} 

[HttpPost] 
     public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, int PartnerID = 0) 
     //public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, FormCollection collection) 
     { 
      int count =0; 
      IList<Partner> p = r.ListPartners(); 
      ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name", PartnerID); 
      //make sure files were selected for upload 
      if (fileUpload != null) 
      { 
       for (int i = 0; i < fileUpload.Count(); i++) 
       { 
        //make sure every file selected != null 
        if (fileUpload.ElementAt(i) != null) 
        { 
         count++; 
         var file = fileUpload.ElementAt(i); 
         if (file.ContentLength > 0) 
         { 
          var fileName = Path.GetFileName(file.FileName); 
          // need to modify this for saving the files to the server 
          var path = Path.Combine(Server.MapPath("/App_Data/uploads"), Guid.NewGuid() + "-" + fileName); 
          file.SaveAs(path); 
         } 
        } 
       } 
      } 

      if (count == 0) 
      { 
       ModelState.AddModelError("", "You must upload at least one file!"); 
      } 
      return View(); 
     } 
    } 

我試圖弄清楚如何將文件數據在後越來越送過來(如果是)控制器,所以我可以保存文件。

- 最後的更新與答案 -

好這個問題原來是兩倍.. 1問題與@FileUpload和需要設置includeFormTag: false

我發現的另一個問題是,我需要請確保在我的@Html.BeginForm包括FormMethod.Post這是在fileUpload計數保持爲0時發現的。我在螢火蟲上運行了分析器,並指出文件數據實際上沒有發佈。以下是更正後的代碼。

我看來

@model IEnumerable<EpubsLibrary.Models.Partner> 
@{ using (Html.BeginForm("Index", "Epub", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    { 
     @Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None") 
     @FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload") 
     <input type="submit" value="send" id="pickPartner"/> 
    }   
} 

我控制器

[HttpPost] 
public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, int PartnerID = 0)   
{ 
    int count =0; 
    IList<Partner> p = r.ListPartners(); 
    ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name", PartnerID); 
    //make sure files were selected for upload 
    if (fileUpload != null) 
    { 
     for (int i = 0; i < fileUpload.Count(); i++) 
     { 
      //make sure every file selected != null 
      if (fileUpload.ElementAt(i) != null) 
      { 
       count++; 
       var file = fileUpload.ElementAt(i); 
       if (file.ContentLength > 0) 
       { 
        var fileName = Path.GetFileName(file.FileName); 
        // need to modify this for saving the files to the server 
        var path = Path.Combine(Server.MapPath("/App_Data/uploads"), Guid.NewGuid() + "-" + fileName); 
        file.SaveAs(path); 
       } 
      } 
     } 
    } 

    if (count == 0) 
    { 
     ModelState.AddModelError("", "You must upload at least one file!"); 
    } 
    return View(); 
} 

謝謝@Jay和@Vasile Bujac與此您的幫助。

回答

1

設置IncludeFormTag到false,並把它放在你的其他形式using內。

@model IEnumerable<EpubsLibrary.Models.Partner> 
@{ using (Html.BeginForm("Index","Epub")) 
    { 
     @FileUpload.GetHtml(initialNumberOfFiles:1,allowMoreFilesToBeAdded:true,includeFormTag:false, uploadText: "Upload") 

     @Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None") 
     <input type="submit" value="send" id="pickPartner" hidden="hidden"/> 
    }   
} 

更新: 試着改變你的觀點的簽名是:

public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, int PartnerID = 0) 

檢查重載FileUpload.GetHtml,看看是否有設置字段名稱爲您的文件上傳的參數。以前它只是上傳文件,現在是文件和參數,因此命名變得更加重要。

+0

請參閱上面的修改。現在我已經做了這個改變,文件數據似乎不再以同樣的方式通過了。謝謝。 – samack

+0

這是在不發送FormCollection的情況下正確設置partnerID,但由於某種原因,即使我選擇多個文件,fileUpload.C​​ount()仍然爲0。 – samack

+0

與Vasile建議一起,嘗試啓動Fiddler並查看您的文件發佈了哪些值。我會想象如果這些文件在工作之前,他們應該繼續工作。 – Jay

0

您應該對下拉列表和文件輸入使用相同的表單。你可以通過在表單中​​放置FileUpload幫助程序,並將「includeFormTag」參數設置爲false來完成此操作。

@model IEnumerable<EpubsLibrary.Models.Partner> 
@using (Html.BeginForm("Index","Epub")) { 
     @FileUpload.GetHtml(initialNumberOfFiles:1,allowMoreFilesToBeAdded:true,includeFormTag:false, uploadText: "Upload") 
     @Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None") 
     <input type="submit" value="send" id="pickPartner" hidden="hidden"/> 
    }   
+0

請參閱上面的修改。現在我已經做了這個改變,文件數據似乎不再以同樣的方式通過了。謝謝。 – samack

+0

也許這是一個模型綁定問題(您的幫助程序爲您的輸入生成了什麼名稱?),您是否檢查了Request.Files集合以查看是否有數據被傳遞? – Vasea

+0

Request.Files爲空。 – samack