简体   繁体   English

如何从ASP.NET MVC 4网站获取文件和表单值?

[英]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.

相关问题 如何在ASP.NET Webforms网站项目中一起使用Webforms和ASP.NET MVC? - How do I use Webforms and ASP.NET MVC together in an ASP.NET Webforms Website project? 如何在ASP.NET MVC中修改Razor View Engine生成的默认表单值? - How do I modify default form values generated by Razor View Engine in ASP.NET MVC? 如果字段值已无效,如何将其保留在ASP.NET MVC表单中? - How do I keep field values in the ASP.NET MVC form if it has been invalidated? 在ASP.Net MVC控制器中,如何获得一种从JSON和Form-urlEncoded格式识别数据的方法? - In ASP.Net MVC controller, how do I get a method to recognize data from JSON and Form-urlEncoded formats? 如何在asp.net MVC 5中创建表单 - How do I create a form in asp.net MVC 5 如何在ASP.NET MVC中将表单提交到数据库中 - How do I submit a form into a database in asp.net MVC 如何从属性而不是直接在模型中获取ASP.NET MVC中的表单值 - How to get form values in asp.net mvc from properties not directly in the model 如何链接到ASP.NET MVC中的可下载文件? - How do I link to downloadable files in ASP.NET MVC? 我如何在 asp.net 核心 mvc 中获取多个选择列表的所有值 - How do i get all the values of multiple selectlist in asp.net core mvc Asp.Net MVC:如何获取Html.ActionLink以正确呈现整数值? - Asp.Net MVC: How do I get Html.ActionLink to render integer values properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM