有类似的帖子,但它们似乎并不代表我的情况。如果这是重新发布,请提前道歉。
我的观点是
@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();
}
我正在使用Microsoft Web Helpers的文件上传帮助程序来上传文件。我遇到的问题是帮助器创建了一个表单,我还有另一种表单,我需要在同一页面上提交数据。
我以为我可以链接提交按钮,这样当您点击上传时它也会发送其他表单数据,但数据没有被发送。每个表单都独立于另一个表单而没有任何问题,但我需要它们一起工作。任何意见,将不胜感激。
好的,我用
更新了视图@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();
}
}
我想弄清楚文件数据是如何在帖子中发送的(如果是的话),所以我可以保存文件。
- 答案的最终更新 -
问题结果是两倍..第一个问题是@FileUpload
并需要设置includeFormTag: false
我发现的另一个问题是我需要确保在我的@Html.BeginForm
中包含FormMethod.Post
这是在fileUpload计数保持为0时发现的。我在firebug上运行了探查器并且它指向文件数据实际上没有发布。以下是更正后的代码。
我的观点
@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对此的帮助。
答案 0 :(得分: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
的重载并查看是否有参数设置文件上传的字段名称。以前只是上传的文件,现在是文件和参数,因此命名变得更加重要。
答案 1 :(得分: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"/>
}