在单个视图/表单中处理 2 个按钮提交操作 - ASP.NET MVC 2 RTM

发布于 2024-08-24 17:50:18 字数 552 浏览 7 评论 0原文

我有一个视图,用户可以在其中将文件上传到服务器。

在此视图中,我还有 2 个按钮:一个用于上传文件,另一个用于下载最后导入的文件。

在我的控制器中,我创建了 2 个操作方法:导入和导出。

我如何设法将每个按钮点击重定向到控制器中正确的操作方法?

我尝试过 Html.ActionLink:

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

Html.ActionLink 没有成功。操作链接将我带到正确的操作方法,但它们正在生成 GET 请求。这样Request.Files.Count = 0。

我需要一个POST请求。

注意:最有趣的部分是上传正常,突然停止了。我发现有些人在 FileUpload 任务中遇到同样的问题,其中 Request.Files 始终为空。我认为它是空的,因为你需要向服务器发送邮件。不是吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绾颜 2024-08-31 17:50:18

也许这会给你这样的想法:

视图

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

控制器:

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

导出导入是适当的操作

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

怪我鬧 2024-08-31 17:50:18

您必须使用“multipart/form-data”表单,然后提交该表单。没有操作链接。

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

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>
简单气质女生网名 2024-08-31 17:50:18

要生成上传的 POST 请求,请使用文件输入表单元素,然后按正常方式发回服务器。

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

看看斯科特·汉塞尔曼 (Scott Hanselman) 的这篇博文。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文