Hi zeeshanpas,
Create a class that will be used to pass the object with Array from View to Controller using Ajax.
Set the object and Array in JavaScript and pass in data property using JSON.stringify.
Please refer below sample.
Model
public class Customer
{
public string Name { get; set; }
public string Country { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public JsonResult UpdateCustomers(Param param)
{
string returnVal = string.Empty;
return Json(returnVal, JsonRequestBehavior.AllowGet);
}
public class Param
{
public Customer Customer { get; set; }
public List<Customer> Customers { get; set; }
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div id="divPageLoading"></div>
<input type="button" id="btnUpdate" value="Update" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#btnUpdate').on('click', function () {
var customer = { Name: "John Hammond", Country: "United States" };
var customers = [];
customers.push({ Name: "Mudassar Khan", Country: "India" });
customers.push({ Name: "Robert", Country: "France" });
manageAddUpadte("/Home/UpdateCustomers", customer, customers);
});
});
function manageAddUpadte(url, customer, customers) {
var parameters = {};
parameters.Customer = customer;
parameters.Customers = customers;
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(parameters),
contentType: "application/json; Charset:utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (data) {
alert(data.responseText);
}
});
}
</script>
</body>
</html>
Screenshot