i'm creating mvc 4 application call function in controller js file using ajax.
when call function ajax, calling respective function properly. neither success nor error function not firing . me out correct mistake?
i read data database convert json format , write .js file , thereafter success function fired off. me solve this. in advance.
here code.
$.ajax({ //url: '@url.action("getjsondata","home")', url: "home/getjsonhugedata1", //data: "{}", type: "get", //contenttype: 'application/json', //datatype: "json", success: function () { alert(); alert('success getjsonhugedata'); loaddata(data); }, error:function(){ alert('error'); } });
controller:
public jsonresult getjsonhugedata() { var users = getusershugedata(); string json = "var datasource="; json += jsonconvert.serializeobject(users.toarray()); system.io.file.writealltext(server.mappath("/scripts/newdata.js"), json); return json(users, jsonrequestbehavior.allowget); } private list<usermodel> getusershugedata() { var userslist = new list<usermodel>(); usermodel user; list<dummydata> data = new list<dummydata>(); using (database1entities dataentity = new database1entities()) { data = dataentity.dummydatas.tolist(); } (int = 0; < data.count; i++) { user = new usermodel { id = data[i].id, productname = data[i].productname, revenue = data[i].revenue, inyear = data[i].inyear.year }; userslist.add(user); } }
i believe browser block file downloaded via ajax, because javascript cannot interact disk. if want working, have using form post.
@using (html.beginform("action", "controller", formmethod.post, new { id = "downloadform" })) { ... form data here if had any... <button type="submit">download</button> }
you return filestreamresult contents of file downloaded.
public actionresult action(formmodel model) { // work data file , return file result browser. return new filestreamresult(new memorystream(filedata), "text/csv") // set document type valid file { filedownloadname = "users.csv" }; }
Comments
Post a Comment