【问题标题】:ASP.NET MVC3 Sending Multiple forms from the same pageASP.NET MVC3 从同一页面发送多个表单
【发布时间】:2011-07-27 20:48:36
【问题描述】:

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

我的看法是

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

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案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 的重载,看看是否有参数可以设置文件上传的字段名称。以前只是上传文件,现在是文件和参数,所以命名变得更加重要。

    【讨论】:

    • 请参阅上面的编辑。既然我进行了更改,文件数据似乎不再以相同的方式传递。谢谢。
    • 这是在不发送 FormCollection 的情况下正确设置了 partnerID,但由于某种原因,即使我选择了多个文件,fileUpload.C​​ount() 仍然为 0。
    • 连同 Vasile 的建议,尝试启动 Fiddler 并查看您的文件发布了哪些值。我想如果这些文件以前可以工作,它们应该可以继续工作。
    • 抱歉我不熟悉 Fiddler 它是做什么的?
    • Fiddler 是一个调试工具,它将捕获在浏览器和服务器之间来回发送的 Web 数据包。非常适合调试此类问题。
    【解决方案2】:

    您应该对下拉列表和文件输入使用相同的表单。您可以通过将 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"/>
       }        
    

    【讨论】:

    • 请参阅上面的编辑。既然我进行了更改,文件数据似乎不再以相同的方式传递。谢谢。
    • 可能是模型绑定问题(您的助手会为您的输入生成什么名称?),您是否检查过 Request.Files 集合以查看是否传递了任何数据?
    猜你喜欢
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多