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

前端 未结 3 1364
感动是毒
感动是毒 2021-01-06 17:47

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

相关标签:
3条回答
  • 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

    0 讨论(0)
  • 2021-01-06 18:02

    To generate a POST request for the upload, use the File Input form element and just post back to the server ala normal.

    http://www.w3schools.com/jsref/dom_obj_fileupload.asp

    Have a look at this blog post from Scott Hanselman. http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx

    0 讨论(0)
  • 2021-01-06 18:09

    You have to use a "multipart/form-data" form, 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>
    
    0 讨论(0)
提交回复
热议问题