javascript - Controller not receiver parameter passed by ajax POST in .Net MVC -
i want make ajax shopping cart, getcarts , addcart working, removerow not receiver parameter "strms". when alert(strms) in removerow(strms) js function, show right value of book id (equal 8). in cartcontroller/removerow debug, strms value null. think it's can problem routing, think route configs right! please me.
the view _layout.cshtml, contain js code controller cartcontroller
my routeconfig.cs
public class routeconfig { public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "sach", action = "detail", id = "7" } ); routes.maproute( "cart", "{controller}/{category}/{ms}/{sl}", new { controller = "cart", action = "addcart", ms = urlparameter.optional, sl = urlparameter.optional } ); routes.maproute( "cartremoverow", "{controller}/{action}/{ms}", new { controller = "cart", action = "removerow", ms = urlparameter.optional }); } } cartcontroller.cs
public class cartcontroller : controller { list<cartitem> listcart; [httppost] public actionresult getcarts() { listcart = cart.getcart(session); return partialview("cart", listcart); } public void addcart(string ms, string sl) { int masach = int.parse(ms); int soluong = int.parse(sl); cart.addcart(masach, soluong, session); //return partialview("default"); } public void removerow(string strms) { int ms1 = int.parse(strms); var sach = new sach() { masach = ms1 }; cart.removecart(sach, true); } } ajax js code
<script type="text/javascript"> function showcart() { // alert("1"); //load content cartcontroller $(function () { //alert("2"); $.ajax({ type: "post", url: '@url.action("getcarts", "cart")', success: function (data) { var result = data; $('#mycart').html(result); //alert($('#mycart').html()); } }); }); $("#mycart").modal('show'); } function addcart(ms, sl) { var masach = []; masach.push(ms); masach.push(sl); // alert(masach); $(function () { $.ajax({ type: 'post', url: '@url.action("addcart", "cart")/'+ms+'/'+ $("#soluong").val(), success: function (data) { showcart(); } }); return false; }); } function removerow(strms){ $(function () { // alert(strms); $.ajax({ type: 'post', url: '@url.action("removerow", "cart")/' + strms, success: function (data) { showcart(); } }); }); } </script>
try passing parameter in data property, have multiple examples not @ home right now.
function removerow(this_strms){ $(function () { // alert(strms); $.ajax({ type: 'post', url: '@url.action("removerow", "cart")', data: { strms: this_strms}, success: function (data) { showcart(); } }); }); } i pass c# objects/classes parameters this
public void removerow(cartobject strms) { int ms1 = strms.ms1; var sach = new sach() { masach = ms1 }; cart.removecart(sach, true); } in js this
var thiscartobject= { ms1: 1, otherproperty: 0.4, otherproperty2: "description" }; function removerow(thiscartobject){ $(function () { // alert(strms); $.ajax({ type: 'post', url: '@url.action("removerow", "cart")', data: json.stringify({ strms: thiscartobject}), contenttype: 'application/json', datatype: 'json', success: function (data) { showcart(); } }); }); } if going try this, try without! custom route defined in registerroutes class first.
Comments
Post a Comment