开发者

ASP.Net MVC File Upload Post Parameters

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


While there are a number of ways to achieve what you want, I would recomend something like the following (since it's the simplest):

Change your action method signature to:

public ActionResult AddCustomers(int BatchID, HttpPostedFileBase SourceFile) 

And change the rendered view to:

<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="hidden" value="<%: Model.BatchId %>" id="BatchID" name="BatchID" />
        <input type="file" id="SourceFile" name="SourceFile" />
        <br />
        <input type="submit" value="Send" name="btnUpload" id="Submit1" />
    </form>

</asp:Content>

This ensures that your BatchId value gets round-tripped along with the file.

More advanced solutions (if this does not fit your needs) could include:

  • adding the BatchId in the URL that the form gets posted to.
  • using Session state to store the id.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜