jquery - Call action of another controller and return its result to View -
i have scenario need following functionality:
in view have call as: $.ajax({ type: "post", async: false, datatype: 'json', url: "controllera/actiona", data: { var1: some_value }, success: function (data) { if (data == true) { form.submit(); } else if (data == false) { } }); // in controllera public jsonresult actiona(string var1) { /* manipulation , calculations */ _slist = redirecttoaction("actionc", "controllerb", new { var1 = some_value}); string = _slist.first().tostring(); return redirecttoaction("actionb", "controllerb", new { var1 = var2 }); } // in controllerb public jsonresult actionb(string var1) { /* manipulation , calculations */ return json(false, jsonrequestbehavior.allowget); } public selectlist actionc(string var1) { /* manipulation , calculations */ session["string"] = some_value; return new selectlist(_storeordertimedictionarylist, "value", "key"); }
i need jsonresult in view page, problems as:
- as redirecttoaction returns redirecttorouteresult can't directly return jsonresut
- as need session in actionc can't instantiate controller , call action.
this may not best approach...
its hard tell, drying controllers, , moving out business logic may help. looks want maintain functionality of actions b, , c.
$.ajax({ type: "post", async: false, datatype: 'json', url: "controllera/actiona", data: { var1: some_value }, success: function (data) { if (data == true) { form.submit(); } else if (data == false) { } }); public class calculationsa { public void docalculation() {} } public class calculationsb { public void docalculation() {} } public class calculationsc { public iqueryable<somethign> docalculation() {} } //_a declared in controller calculationsa //_b declared in controller b calculationsb //_c declared in controller c calculationsc // in controllera public jsonresult actiona(string var1) { _a.docalculation(); _slist = _b.docalculation().first().tostring(); session["string"] = some_value; _c.docalculation(); /* other logic... */ return json(retval, jsonrequestbehavior.allowget); } // in controllerb public jsonresult actionb(string var1) { _b.docalculation(); return json(false, jsonrequestbehavior.allowget); } public selectlist actionc(string var1) { _c.docalculation(); session["string"] = some_value; return new selectlist(_storeordertimedictionarylist, "value", "key"); }
btw, should check out ninject, castle windsor, structure map, or other di/ioc container, test logic (and make dryer). try searching ninject asp.net mvc 2 tutorial
Comments
Post a Comment