Reputation: 75
I want to return an array from controller to view by ajax call,
and my array is a generic array (a list of objects such as an instance of Employee
class).
I want loop on result and have access to objects property such as Name
,
and I do not know, how can I return generic array for result from C# action (json or ...) ,
What is the return type of action?
This is my code:
$.ajax({
url: '@Url.Action("EditDayRequest", "Message")',
type: 'Post',
cache: false,
data: { IsChecked: $(this).is(':checked')},
success: function (result) {
// i want to loop here on returned array and get values
}
Upvotes: 2
Views: 8357
Reputation: 218822
Assuming your Customer ViewModel looks like this
public class CustomerVM
{
public string Name { set;get;}
public string JobTitle { set;get;}
}
You can return Json from your Action method using the Json
method.
[HttpPost]
public ActionResult EditDayRequest()
{
var customerArray=GetCustomerArrayFromSomewhere();
return Json(new { Items=customerArray.ToList()});
}
And in your success callback, you can loop thru the Items
success:function(result){
$.each(result.Items,function(index,item){
alert(item.Name);
alert(item.JobTitle);
});
}
Upvotes: 2