简体   繁体   English

asp.net MVC。 没有重定向就不能使用void方法

[英]asp.net MVC. Can't use void method without redirect

I have a form and void post method. 我有一个表单和无效的帖子方法。

<form id="frm_loadFile" action="/Home/LoadFileWhithChanges" enctype="multipart/form-data" method="post" style="margin-top: 40px;">
                <input type="file" style="width: 100%;" name="upload" value="Choose file"><br><br>
                <input style="width: 115px;" type="submit" value="Upload">
</form>

    [HttpPost]
    public void LoadFileWhithChanges(HttpPostedFileBase upload)
    {
                if (upload == null)
                {
                    Response.Write(@"<script language='javascript'>alert('No file')</script>");
                    Response.Flush();
                    Response.End();

                    return;
                }
                else
                {
                   //...
                   Response.Write(@"<script language='javascript'>alert('All done')</script>");
                    Response.Flush();
                    Response.End();

                    return;
                }
}

But when it calls Response.Write after alert my page redirects 但是当它在警报后调用Response.Write时,我的页面重定向

I want stay on this page after response.write/ How can I do this? 我想在response.write/之后停留在该页面上response.write/怎么做?

You are posting a form, and receive a response. 您正在发布表单,并收到响应。 It doesn't matter that your action is void . 你的行为void没关系。 The browser doesn't care how the response was generated on the server. 浏览器不在乎服务器上如何生成响应。 It's only course of action is to display the result. 唯一的行动就是显示结果。 If you want to stay in the same page, you should either: 如果要保留在同一页面上,则应该:

  1. return the same page from the action 从动作返回相同页面
  2. post the form in AJAX, which, in the case of posting a file, requires a special technique, involving iframes. 在AJAX中发布表单,如果发布文件,则需要一种特殊的技术,涉及iframe。 You can look it up on the web. 您可以在网上查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM