Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC3 Sending Multiple forms from the same page
    primarykey
    data
    text
    <p>There are similar posts out there but they do not seem to represent my situation. Apologies in advance if this is a re-post.</p> <p>I have my view as </p> <pre><code>@FileUpload.GetHtml(initialNumberOfFiles:1,allowMoreFilesToBeAdded:true,includeFormTag:true, uploadText: "Upload" ) @model IEnumerable&lt;EpubsLibrary.Models.Partner&gt; @{ using (Html.BeginForm("Index","Epub")) { @Html.DropDownList("PartnerID", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Partners, "None") &lt;input type="submit" value="send" id="pickPartner" hidden="hidden"/&gt; } } &lt;script type="text/javascript"&gt; $(".file-upload-buttons input").click(function () { $("#pickPartner").click(); }); &lt;/script&gt; </code></pre> <p>my controller is </p> <pre><code>[HttpPost] public ActionResult Index(IEnumerable&lt;HttpPostedFileBase&gt; 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&lt;Partner&gt; 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 &lt; fileUpload.Count(); i++) { //make sure every file selected != null if (fileUpload.ElementAt(i) != null) { count++; var file = fileUpload.ElementAt(i); if (file.ContentLength &gt; 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(); } </code></pre> <p>I am using the file upload helper from Microsoft Web Helpers to upload files. The problem I am having is the helper created a form and I have another form I need to submit data from as well on the same page. </p> <p>I thought I could link the submit buttons so that when you click upload it also sent the other form data but the data is not being sent. Each form works independently of the other with no issue but I need them to work together. Any advice would be appreciated. </p> <p>Ok I updated the view with </p> <pre><code>@model IEnumerable&lt;EpubsLibrary.Models.Partner&gt; @{ using (Html.BeginForm("Index","Epub")) { @Html.DropDownList("PartnerID", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Partners, "None") @FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload") &lt;input type="submit" value="send" id="pickPartner"/&gt; } } </code></pre> <p>But now the file data does not seem to be getting passed anymore. </p> <p>-- Update -- </p> <p>I have made the following changes. The view now looks like </p> <pre><code>@model IEnumerable&lt;EpubsLibrary.Models.Partner&gt; @{ using (Html.BeginForm("Index", "Epub", new { enctype = "multipart/form-data" })) { @Html.DropDownList("PartnerID", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Partners, "None") @FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload") &lt;input type="submit" value="send" id="pickPartner"/&gt; } } </code></pre> <p>and the controller </p> <pre><code>[HttpPost] public ActionResult Index(IEnumerable&lt;HttpPostedFileBase&gt; fileUpload, int PartnerID = 0) //public ActionResult Index(IEnumerable&lt;HttpPostedFileBase&gt; fileUpload, FormCollection collection) { int count =0; IList&lt;Partner&gt; 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 &lt; fileUpload.Count(); i++) { //make sure every file selected != null if (fileUpload.ElementAt(i) != null) { count++; var file = fileUpload.ElementAt(i); if (file.ContentLength &gt; 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(); } } </code></pre> <p>I am trying to figure out how the file data is getting sent over in the post (if it is) so I can save the files. </p> <p>-- Final Update with Answer -- </p> <p>Well the problem turned out to be two fold.. 1st the issue with <code>@FileUpload</code> and needing to set <code>includeFormTag: false</code></p> <p>The other problem I discovered was I needed to make sure in my <code>@Html.BeginForm</code> I included <code>FormMethod.Post</code> This was discovered when the fileUpload count kept coming back as 0. I ran the profiler on firebug and it pointed out that the file data was not actually getting posted. Here is the corrected code below. </p> <p>my view</p> <pre><code>@model IEnumerable&lt;EpubsLibrary.Models.Partner&gt; @{ using (Html.BeginForm("Index", "Epub", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.DropDownList("PartnerID", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Partners, "None") @FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload") &lt;input type="submit" value="send" id="pickPartner"/&gt; } } </code></pre> <p>my controller</p> <pre><code>[HttpPost] public ActionResult Index(IEnumerable&lt;HttpPostedFileBase&gt; fileUpload, int PartnerID = 0) { int count =0; IList&lt;Partner&gt; 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 &lt; fileUpload.Count(); i++) { //make sure every file selected != null if (fileUpload.ElementAt(i) != null) { count++; var file = fileUpload.ElementAt(i); if (file.ContentLength &gt; 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(); } </code></pre> <p>Thank you @Jay and @Vasile Bujac for your help with this. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload