-4

Please share the ajax code for storing multiple checkboxes checked values to an array and pass to controller without submit button in MVC4. Please guide me. Thanks in advance

1
  • have you looked online yet? Commented Nov 25, 2016 at 10:00

1 Answer 1

-1

User Jquery Ajax to call your controller's method.

Controller Code :

 public class CustomerController : Controller 
 {
  public ActionResult Index() 
  {
   return View();
  }

  [HttpPost]
  public ActionResult UpdateOrder()
  {
  // some code
  return Json(new { success = true, message = "Order updated successfully"    }, JsonRequestBehavior.AllowGet);
   }
  } 

Jquery code :

$.ajax({
url: '@Url.Action("UpdateOrder")', // to get the right path to controller from TableRoutes of Asp.Net MVC
dataType: "json", //to work with json format
type: "POST", //to do a post request 
contentType: 'application/json; charset=utf-8', //define a contentType of your request
cache: false, //avoid caching results
data: {}, // here you can pass arguments to your request if you need
success: function (data) {
     // data is your result from controller
    if (data.success) { 
        alert(data.message);
    }
},
error: function (xhr) {
    alert('error');
}
});

Above code has taken as reference from here :

http://www.c-sharpcorner.com/UploadFile/337dfd/useful-way-to-call-controller-actions-from-html-using-jquery/

jQuery to call Action Method in ASP.NET MVC C# by Ajax

Hope this would help.

Sign up to request clarification or add additional context in comments.

2 Comments

but i cannot find anywhere of sending checked values to controller in your answer @Naresh Parmar
You have to get the checkbox value using Jquery and pass it in data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.