asp.net mvc - How to prevent redirect to new page on form submit -
i trying upload form containing multiple files server, request going correct action , getting data files coming null values.
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 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>
- it working without "ajaxsubmit" page redirecting "http://localhost:xxxx/mobile/home/fileupload", don't want page come up, wanted catch response , based on
my controller
public actionresult fileupload(formcollection fc, list<httppostedfilebase> files) { //some functionilty save data working perfectely return json(succesmessage, jsonrequestbehavior.allowget); }
*note - using jquery mobile there no views in project
thanks accepted answer on this post , stephen muecke suggesting me on question.
what did-
- removed action attribute form tag
- used on click instead of submit
- used ajax post data given in reference link.
my edited js shown below
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!"); } });
changed controller action -
public actionresult fileupload(uploadmodel fm, list<httppostedfilebase> files) { //some functionilty save data working perfectely return json(succesmessage, jsonrequestbehavior.allowget); }
- uploadmodel model having same name used in form
Comments
Post a Comment