I am trying to go to a view with a speicifed batchId parameter wrapped in a ViewModel, pick a file to upload, get the uploaded file back and store the file data w/ the associated BatchId value in a database.
When the form is submitted I don't know how to get back the viewmodel and the PostedFileBase so that I can get the BatchId value.
I need the batchId value to associate it with the data I am storing in the database.
I have the following Action Method in my Controller to allow adding new customers to the specified batch by means of a file upload and import:
public ActionResult AddCustomers(int batchId)
{
var viewModel = new AddCustomersViewModel() { BatchId = batchId, //other view model properties };
return View(viewModel);
}
My view is strongly typed to that ViewModel:
Inherits="System.Web.Mvc.ViewPage<TestExcelImport.Areas.Admin.ViewModels.AddCustomersViewModel>
and has the following for the file upload:
<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>
My HttpPost Action Method is defined as:
[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");
}
I have tried adding a AddCustomersViewModel in the HttpPost return method parameter list but it is always null. I can read/parse the uploaded file just fine, just can't get back which BatchId it was a part of.
Anybody see what I'm doing wrong?
Thanks