ASP.NET MVC POST 仿AJAX静态刷新表单上传文件以及传递数据

来源:互联网 发布:韩版时尚包包批发淘宝 编辑:程序博客网 时间:2024/06/01 08:39

 

View:测试发现公司之前MVC写的ajax post页面数据的功能有问题 重新修改成 form表单post 上传文件

    不使用iframe会发生每次post提交会打开新的控制器页面 而不是在当前页面做处理 找到之前用的在HTML页面post上传的功能做修改

<form   method="POST" id="postFile" enctype="multipart/form-data" target="ifameOne">
                              
                                <table style="width: 100%; background-color: White; margin-bottom: 10px;">
                                      <td id="files" style="padding-top: 10px; text-align: left;">
                                          
                                            <input type="text" maxlength="200" class="description" autocomplete="off" id="txt1"
                                                name="txt1" /><input style="width: 228px;" type="file" class="ff" name="FileUpload1" /><br />
                                            <input type="text" maxlength="200" class="description" autocomplete="off" id="txt2"
                                                name="txt2" /><input style="width: 228px;" type="file" class="ff" name="FileUpload2" /><br />
                                            <input type="text" maxlength="200" class="description" autocomplete="off" id="txt3"
                                                name="txt3" /><input style="width: 228px;" type="file" class="ff" name="FileUpload3" /><br />
                                    </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" align="right" style="height: 40px; padding-right: 15px; padding-top: 20px;">
                                           <input type="submit" value="submit" name="Submit" onclick="checkform()"/>
                                         
                                        </td>
                                    </tr>
                                </table>
                                </form>

  

 <iframe id="ifameOne" name="ifameOne" src="/LogisticMessage/PostFile" style="display:none;"></iframe>

 

控制器代码:

 public ActionResult PostFile()
        {
             ContentResult content = new ContentResult();
            content.ContentType = "text/html";
            if (returnStatue == "Save")
            {
                content.Content = "<script type='text/javascript' language='javascript'>parent.callback(true);</script>";
            }
            else
            {
                content.Content = "<script type='text/javascript' language='javascript'>parent.callback(true,false);</script>";
            }
            returnStatue = "";
            return content;
           
        }

后台定义变量全局returnStatue  当第一次加载页面的时候returnStatue值为空 控制返回parent.callback(true,false);页面不做处理,当有增删改的操作的时候给returnStatue赋值为Save 控制器返回parent.callback(true);页面提示操作信息

js

 function callback(str,mes) {
           
            if (mes == null) {
             
                if (str == true) {
                   
                    alert('The operation was successful');
                     $("#files").find("input").val('');
                     $('#dt').datagrid("reload");
                }
            } 
        }

 

ps:才用了几天的MVC 不知道是不是我技术太烂很多方法不知道 所以采用了这种办法 ,有更好的办法的朋友求指点 我QQ80828602 可以教教我 3Q

原创粉丝点击