Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling 2 buttons submit Actions in a single View/Form - ASP.NET MVC 2 RTM

I have a View in which the user is able to upload a file to the server.

In this view I also have 2 buttons: one to Upload a file and other to Download the last file imported.

In my Controller I created 2 action methods: Import and Export.

How could I manage to redirect each button click to the proper action method in my Controller?

I have tried Html.ActionLink:

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

Html.ActionLink didn't do the trick. The action links were taking me to the right Action methods but they were generating a GET request. This way Request.Files.Count = 0.

I need a POST request.

Note: the most intriguing part is that the upload was working and all of sudden it stopped working. I've seen that some people are having the same problem with FileUpload tasks in which the Request.Files is always Empty. I think it's empty because you need a post to the server. Isn't it?

like image 728
Leniel Maccaferri Avatar asked Jan 26 '26 20:01

Leniel Maccaferri


1 Answers

maybe this will give u the 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);
            }
        }

the Export and Import are the appropriateactions

like image 170
CoffeeCode Avatar answered Jan 29 '26 11:01

CoffeeCode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!