gpt4 book ai didi

c# - ASP.NET MVC3 从同一页面发送多个表单

转载 作者:行者123 更新时间:2023-11-30 18:03:46 26 4
gpt4 key购买 nike

那里有类似的帖子,但它们似乎并不代表我的情况。如果这是重新发布,请提前致歉。

我有我的看法

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

我的 Controller 是

[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 的文件上传助手来上传文件。我遇到的问题是助手创建了一个表单,我有另一个表单需要在同一页面上提交数据。

我想我可以链接提交按钮,这样当你点击上传时它也会发送其他表单数据,但数据没有被发送。每种形式都可以独立工作,没有问题,但我需要它们一起工作。任何意见,将不胜感激。

好的,我用

更新了 View
@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"/>
}
}

但现在文件数据似乎不再通过了。

-- 更新--

我做了以下更改。 View 现在看起来像

@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"/>
}
}

和 Controller

[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"/>
}
}

我的 Controller

[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 在这方面的帮助。

最佳答案

将 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"/>
}
}

更新:尝试将您的 View 签名更改为:

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

检查 FileUpload.GetHtml 的重载,看看是否有参数来设置文件上传的字段名称。以前只是上传文件,现在是文件和参数,所以命名变得更加重要。

关于c# - ASP.NET MVC3 从同一页面发送多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6851034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com