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? Right now I have ...
所以我的最后一个问题是......如何更改HTML.BeginForm块以传递文件和表单值?现在我有......
@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?
编辑我的编辑似乎我不需要做任何更改......调试器显示文件和表单都是非空的。凉!是对的吗?
2 个解决方案
#1
6
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. you can also pass in the HTML name.
您应该能够在[HttpPost]操作中传入2个参数。您也可以传入HTML名称。
Edit: I also had problems with forms in ASP.net. I suggest looking into this blog post by Scott Allen. http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
编辑:我在ASP.net中也遇到了表单问题。我建议查看Scott Allen撰写的这篇博文。 http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
#2
1
Use a ViewModel type that contains both the posted files and form values, or use the HttpRequest
(accessed via the Controller.Request
property) object, which has .Form[key]
for POST values and .Files[key]
for posted files.
使用包含已发布文件和表单值的ViewModel类型,或使用HttpRequest(通过Controller.Request属性访问)对象,其中POST值为.Form [key],发布文件为.Files [key]。
#1
6
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. you can also pass in the HTML name.
您应该能够在[HttpPost]操作中传入2个参数。您也可以传入HTML名称。
Edit: I also had problems with forms in ASP.net. I suggest looking into this blog post by Scott Allen. http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
编辑:我在ASP.net中也遇到了表单问题。我建议查看Scott Allen撰写的这篇博文。 http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
#2
1
Use a ViewModel type that contains both the posted files and form values, or use the HttpRequest
(accessed via the Controller.Request
property) object, which has .Form[key]
for POST values and .Files[key]
for posted files.
使用包含已发布文件和表单值的ViewModel类型,或使用HttpRequest(通过Controller.Request属性访问)对象,其中POST值为.Form [key],发布文件为.Files [key]。