1

From my controller in mvc 4, I request a collection of objects from my model in database using entity framework:

    [HttpPost]
    public ActionResult RequestDbObjects()
    {
        List<MyObjectType> objCollection;
        using (DataContext context = new DataContext())
        {
            objCollection= context.MyObjects.Where(o => o.TypeId == 1).OrderBy(k => k.Name).ToList();
        }

        return Json(objCollection);
    }

Model:

[Table("MyObjects")]
public class MyObjectType
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [ForeignKey("Type")]
    public int TypeId { get; set; }
    public virtual Type Type{ get; set; }
}

[Table("Type")]
public class Type
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}

And in the view, using a script:

   function recollectData() {         
       $.ajax({
           url: "/Controller/RequestDbObjects/",
           type: 'POST',
           success: function (dataCollection) {
              for(var obj in dataCollection)
              {
                 var value = dataCollection[obj];
              }
           },
           error: function () {
               alert('Cannot retrieve the data');              
           }
       });             
   };

In the above controller, first o all I call to the action in the controller which request data from database using entity framework, then from the list of objects returned to the script, I want to iterate on them, and for each of them read some attributes and do some stuff but I do not know how to do it.Ideas?

2
  • 1
    Shouldn't you retun a JsonResult instead? stackoverflow.com/questions/16686367/… Commented Sep 21, 2013 at 12:00
  • Sincerely, I do not know exactly what to return, maybe it would be the correct.Anyway what I want is to be able to iterate on the collection from the script and access some attributes of each object in the script in order to do some stuff.Ideas?Thanks for your suggestion. Commented Sep 21, 2013 at 12:03

2 Answers 2

1

Your success handler returns the data. If it is a JSON object you may read it like that:

success: function (data) {
    for(var property in data) {
        var value = data[property];
    } 
}

If you return an array:

success: function (data) {
    for(var i=0; item=data[i]; i++) {

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

11 Comments

I do not understand at all I guess data in the loop corresponds to the list of objects returned by de controller, am i right?
Yes, that's what I mean.
so you are iterating over, let me say, each object of the list, the value as I understand should be the current object and how to access the attribute of the current object? value.¿?
With just a dot. I.e. value.name for example.
I do not know why but after returning from the controller, the script is handling an error, so enter in the error option rather than in the success one...
|
1

You must return an ActionResult, not a List:

[HttpPost]
public ActionResult RequestDbObjects()
{
    List<MyObjectType> objCollection;
    using (DataContext context = new DataContext())
    {
        objCollection= context.MyObjects.Where(o => o.TypeId == 1).OrderBy(k => k.Name).ToList();
    }

    return Json(objCollection);
}

1 Comment

Yes, I have updated. In fact, I convert the data returned by the query to a list that I store into objCollection, finally I return this object as Json.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.