ASP.Net MVC file upload options

I am trying to go to the view using the specified batchId parameter enclosed in ViewModel, select the file to upload, return the downloaded file and save the file data with the associated BatchId value in the database.

When the form is submitted, I don’t know how to return the viewmodel and AddFileBase model to get the BatchId value.

I need a batchId value to associate it with the data that I store in the database.

I have the following action method in my controller, which allows me to add new clients to the specified group by loading and importing files:

public ActionResult AddCustomers(int batchId)
{
    var viewModel = new AddCustomersViewModel() { BatchId = batchId, //other view model properties };
        return View(viewModel);
}

My opinion is strongly typed for this ViewModel:

Inherits="System.Web.Mvc.ViewPage<TestExcelImport.Areas.Admin.ViewModels.AddCustomersViewModel>

and has the following values ​​for downloading the file:

<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");
    }

AddCustomersViewModel HttpPost, . / , , BatchId .

- , ?

+3
1

, - ( ):

:

public ActionResult AddCustomers(int BatchID, HttpPostedFileBase SourceFile) 

:

<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>

, BatchId .

( ) :

  • BatchId URL-, .
  • .
+6

Source: https://habr.com/ru/post/1762354/


All Articles