请分享ajax代码,用于将多个复选框选中的值存储到数组中,并传递给控制器,而无需MVC4中的提交按钮。请指导我。 提前致谢
答案 0 :(得分:-1)
用户Jquery Ajax来调用你的控制器的方法。
控制器代码:
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代码:
$.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');
}
});
上面的代码从这里开始作为参考:
jQuery to call Action Method in ASP.NET MVC C# by Ajax
希望这会有所帮助。