2

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
}

1 Answer 1

2

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);
   });

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

Comments

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.