[英]How do I get files AND form values from an ASP.NET MVC 4 website?
I have an ASP.NET MVC Website that has a dropdown list that is being created using this in the view... 我有一个ASP.NET MVC网站,其中有一个下拉列表,正在视图中使用它创建...
@Html.DropDownList("Programs")
Programs is populated from a Business Object collection and stuffed into the ViewBag in the index action on the Home Controller... 程序从Business Object集合填充,并填充到Home Controller上的索引操作中的ViewBag中......
\\get items...
ViewBag.Programs = items;
The view also has potentially three files I am getting like this in the same view... 该视图还有三个我在同一视图中得到的文件...
<input type="file" name="files" id="txtUploadPlayer" size="40" />
<input type="file" name="files" id="txtUploadCoaches" size="40" />
<input type="file" name="files" id="txtUploadVolunteers" size="40" />
All of the aforementioned controls are contained in a Form that is created in the view using... 所有上述控件都包含在使用...在视图中创建的表单中
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<!-- file and other input types -->
<input type="submit" name="btnSubmit" value="Import Data" />
}
My problems is that I cannot find a way to process all of the files AND reference the form fields. 我的问题是我找不到处理所有文件的方法并引用表单字段。
Specifically, I need to know what Program the user selected from the dropdown. 具体来说,我需要知道用户从下拉菜单中选择的程序。
I can process the files using this code with no problem... 我可以使用这段代码处理文件没有问题...
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
//public ActionResult Index(FormCollection form)
{
_tmpFilePath = Server.MapPath("~/App_Data/uploads");
if (files == null) return RedirectToAction("Index");
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(_tmpFilePath, fileName);
if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
_file = file;
file.SaveAs(path);
break; //just use the first file that was not null.
}
}
//SelectedProgramId = 0;
//DoImport();
return RedirectToAction("Index");
}
But I cannot figure how to ALSO get access to the POST form values especially the Programs dropdown selected value (and for the record there is also a checkbox that I cannot read the value from.) Fiddler shows me that the Response has the file references AND the selected program but I cannot figure out how to get them out of the POST using ASP.NET MVC. 但我无法弄清楚如何获取POST表单值,尤其是“程序”下拉列表选择值(对于记录,还有一个复选框,我无法从中读取值。)Fiddler向我显示响应具有文件引用AND选定的程序,但我无法弄清楚如何使用ASP.NET MVC将它们从POST中取出。
I know this question is pretty basic but I am stilling learning the whole web/http thing not just MVC. 我知道这个问题非常基本,但我还在学习整个web / http的东西,而不仅仅是MVC。
EDIT Thanks for your answers. 编辑感谢您的回答。 I had the thought that the answer might lie in passing in both the files and the form values into the POST.
我认为答案可能在于将文件和表单值都传递到POST中。
So my last question is ... how do I change the HTML.BeginForm block to pass in both the files and form values? 所以我的最后一个问题是......如何更改HTML.BeginForm块以传递文件和表单值? Right now I have ...
现在我有......
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
//do stuff
}
What should that using statement be to get both form values and files as separate parameters of the ActionResult? 那个using语句应该将表单值和文件作为ActionResult的单独参数来获取?
EDIT MY EDIT 编辑我的编辑
It seems that I don't have to make any changes...the debugger is showing that both files and form are non-null. 似乎我不必进行任何更改......调试器显示文件和表单都是非空的。 Cool!
凉! Is that right?
是对的吗?
I think that this should do it 我认为应该这样做
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files, FormCollection form)
{
//handle the files
//handle the returned form values in the form collection
}
You should be able to pass in 2 parameters in the [HttpPost] action. 您应该能够在[HttpPost]操作中传入2个参数。 you can also pass in the HTML name.
您也可以传入HTML名称。
Edit: I also had problems with forms in ASP.net. 编辑:我在ASP.net中也遇到了表单问题。 I suggest looking into this blog post by Scott Allen.
我建议看一下Scott Allen撰写的这篇博文。 http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
使用同时包含张贴的文件和形式值的视图模型的类型,或使用HttpRequest
(通过访问Controller.Request
对象,它具有性质) .Form[key]
为POST值和.Files[key]
张贴的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.