如何防止在表单提交上重定向到新页面

时间:2016-02-08 09:42:20

标签: asp.net-mvc forms jquery-mobile ajaxsubmit

我正在尝试将包含多个文件的表单上传到我的服务器,请求将更正Action并且我也获得了一些数据,但所有文件都带有空值。



var file = function(){
this.submitForm = function () {     
  $("#addBrtForm").ajaxSubmit(function (response) {
    if (response === "Barter Uploaded Successfully") {
      alert(response);
      $.mobile.changePage("#p-afterUpload");
      t.somefunction();
    } else {
      alert("Try Again!! Barter Not Uploaded");
    }
});
};
};
hm.files = new file();

//other thing that I tried
 $(function(){
 
  $('#addBrtForm').ajaxForm({
        type: 'POST',
        beforeSubmit: function () {
            return false;
        },
        success: function (response) {
            return false;
            if (response === "Barter Uploaded Successfully") {
              
                alert(response);
                $.mobile.changePage("#p-barter");
                t.setBarterpageTitle('My Barter');
            } else {
                alert("Try Again!! Barter Not Uploaded");
            }           
        }
    });
 });

 <form method="post" action="http://localhost:xxxx/Mobile/Home/FileUpload" enctype="multipart/form-data" data-ajax="false" id="addBrtForm" name="addBrtForm" >
  <input type="text" name="Title" data-role="none"  />
   <input type="text" name="Description" data-role="none" />
   <input type="file" name="files" data-role="none" multiple />
   <input type="file" name="files" data-role="none" multiple />
   <input type="file" name="files" data-role="none" multiple />
   <input type="file" name="files" data-role="none" multiple />
   <input type="file" name="files" data-role="none" multiple />
   <input type="Submit" name="" value="submit" data-role="none" multiple />
   <input type="Button" name="" value="submit" data-role="none" multiple onclick="hm.files.submitForm()"/>
   </form>
&#13;
&#13;
&#13;

  • 没有&#34; ajaxSubmit&#34;但是页面正在重定向到&#34; http://localhost:xxxx/Mobile/Home/FileUpload&#34;,我不希望该页面出现,我只是想抓住我的回复并根据它做一些事情

我的控制器

  public ActionResult FileUpload(FormCollection fc, List<HttpPostedFileBase> files)
    {
      //some functionilty to save data working perfectely 

        return Json(SuccesMessage, JsonRequestBehavior.AllowGet);
    }

*注意 - 因为我正在使用jquery mobile,所以我的项目中没有视图

2 个答案:

答案 0 :(得分:0)

感谢this post Stephen Muecke上接受的答案,建议我查看该问题。

我做了什么 -

  1. 从我的表单标记中删除了操作属性
  2. 用于点击而不是提交
  3. 使用ajax发布参考链接中给出的数据。
  4. 我编辑的js如下所示

     var formdata = new FormData($('#addBrtForm').get(0));
                $.ajax({
                    url: "http://localhost:xxxx/Mobile/Home/FileUpload",
                    type: 'POST',
                    data: formdata,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function (response) {
                        if (response === "File Uploaded Successfully") {
                            alert(response);
                            $.mobile.changePage("#p-afterUpload");
                            t.someFunction();
                        } else {
                            alert("Try Again!! File Not Uploaded");
                        }
                    },
                    error: function (e) {
                        alert("Network error has occurred please try again!");
                    }
                });

    将控制器操作更改为此 -

     public ActionResult FileUpload(UploadModel fm, List<HttpPostedFileBase> files)
    {
      //some functionilty to save data working perfectely 
    
        return Json(SuccesMessage, JsonRequestBehavior.AllowGet);
    }
    

    - UploadModel是我的模型,其名称与我在表单中使用的名称相同

答案 1 :(得分:-1)

id添加到submit按钮。

<input type="Button" name="" id="submit" value="submit" data-role="none" multiple/>

然后在javascript中这样做

         $(function(){
            $('#submit').click(function(e){
                Do Your stuf here
                e.preventDefault();
            });
         }