我有了正被在視圖中使用此創建一個下拉列表中選擇ASP.NET MVC的網站...如何從ASP.NET MVC 4網站獲取文件和表單值?
@Html.DropDownList("Programs")
程序從業務對象集合填充,塞到ViewBag索引在家庭控制器動作......
\\get items...
ViewBag.Programs = items;
的觀點也有潛在的三個文件,我在同樣的觀點,得到了下面......
<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" />
上述所有控件都包含在在視圖中使用創建的窗體...
@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" />
}
我的問題是,我無法找到一個方法來處理所有的文件和參考表單字段。
具體來說,我需要知道用戶從下拉菜單中選擇了哪些程序。
我可以處理使用此代碼沒有問題的文件...
[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");
}
但我想不通怎麼也得進入POST表單值特別程序下拉列表中選擇的值(和備案還有一個複選框,我無法從中讀取值。)Fiddler向我顯示,響應具有文件引用和所選程序,但我無法弄清楚如何使用ASP.NET MVC將它們從POST中取出。
我知道這個問題是非常基本的,但我仍然在學習整個網絡/ http事情不只是MVC。
編輯 感謝您的回答。我認爲答案可能在於將文件和表單值傳遞到POST中。
所以我的最後一個問題是......如何更改HTML.BeginForm塊來傳遞文件和表單值?現在我有...
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
//do stuff
}
我應該在使用的語句是同時獲得表單數據和文件的ActionResult的獨立參數?
編輯我的編輯
看來,我沒有做任何改變......調試器顯示,這兩個文件和表單非空。涼!是對的嗎?