3
我想要去一個ViewModel包裝一個指定的batchId參數的視圖,選擇一個文件上傳,獲取上傳的文件,並存儲文件數據W /關聯的BatchId數據庫中的值。ASP.Net MVC文件上傳後參數
當提交表單時,我不知道如何取回視圖模型和PostsFileBase,以便我可以獲取BatchId值。
我需要將batchId值與我在數據庫中存儲的數據關聯起來。
我在我的控制下操作方法,以允許通過一個文件上傳的方式和進口增加新客戶到指定的批次:
public ActionResult AddCustomers(int batchId)
{
var viewModel = new AddCustomersViewModel() { BatchId = batchId, //other view model properties };
return View(viewModel);
}
我的看法是強類型到視圖模型:
Inherits="System.Web.Mvc.ViewPage<TestExcelImport.Areas.Admin.ViewModels.AddCustomersViewModel>
,並有文件上傳如下:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>AddCustomers Batch ID : <%:Model.BatchId %></h2>
<form action="/Admin/Dashboard/AddCustomers" enctype="multipart/form-data" method="post">
<input type="file" id="SourceFile" name="SourceFile" />
<br />
<input type="submit" value="Send" name="btnUpload" id="Submit1" />
</form>
</asp:Content>
我HttpPost action方法定義爲:
[HttpPost]
public ActionResult AddCustomers(HttpPostedFileBase SourceFile)
{
//int batchId = ??? HOW DO I Get the BatchId
int fileLength = SourceFile.ContentLength; //works!
// read through SourceFile.InputStream and store it in db
//need the associated BatchID though
return RedirectToAction("Index");
}
我已經嘗試添加在HttpPost返回方法參數列表AddCustomersViewModel但它始終是零。我可以讀取/解析上傳的文件,只是無法取回它屬於哪個BatchId。
有人看到我做錯了什麼?
感謝
非常感謝,工作。我對參數映射回來有點困惑。是否有可能讓整個視圖模型在post方法中交還? 我想我的混淆開始b/c我見過的大多數例子都是使用HtmlHelper擴展方法進行表單回發,但由於我使用HtmlHelpers在我的代碼中進行文件上傳,因此將我引入循環。 – jrob 2010-08-30 22:05:43