Handling 2 submit action buttons in one view / form - ASP.NET MVC 2 RTM

I have a view in which a user can upload a file to the server.

In this view, I also have two buttons: one for loading a file and another for loading the last imported file.

In my controller, I created 2 action methods: import and export.

How can I redirect each button to the correct action method in my controller?

I tried Html.ActionLink:

<%= Html.ActionLink("Upload", "Import", "OracleFile")%>
<%= Html.ActionLink("Download", "Export", "OracleFile")%>

Html.ActionLink did not do this trick. The action links led me to the right action methods, but they generated a GET request. Thus Request.Files.Count = 0.

I need a POST request.

Note. The most intriguing part is that the download worked, and suddenly it stopped working. I saw that some people face the same problems with FileUpload tasks in which Request.Files is always empty. I think it is empty because you need a message to the server. Is not it?

+3
source share
3 answers

maybe this will give you an idea:

view

<form enctype="multipart/form-data" method="post" action="/Media/Upload/Photo">
    <input type="file" name="file" id="file" /> 
    <input type="submit"  name= "submitImport" value="Upload" />
    <input type="submit" name = "submitExport"  value="Download" />
</form>

controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Action (FormCollection formCollection)
        {
            if (formCollection["submitImport"] != null)
            {
                return Import(formCollection);
            }
             if (formCollection["submitExport"] != null)
            {
                return Export(formCollection);
            }
        }

Export and Import are appropriate actions

+8
source

You must use the form "multipart / form-data" and submit the form. No actionlink.

<form enctype="multipart/form-data" method="post" action="/Media/Upload/Photo">
    <input type="file" name="file" id="file" /> 
    <input type="submit" value="Upload" />
</form>
+2
source

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


All Articles