views:

51

answers:

2

Hello, I am using mvc and jQuery and I am trying to display someone's profile with some additional institutions that the person belongs to. I am new to this but I've done something like this in ProfileControler:

public ActionResult Institutions(int id)
    {
       var inst = fr.getInstitutions(id);
        return Json(inst);
    }

getInstitutions(id) returns Institution objects(with Name, City, Post Code etc.) Then in a certain View I am trying to get the data with jQuery and display them as follows:

$(document).ready(function () {
        $.post("/Profile/Institutions", { id: <%= Model.Profile.userProfileID %> }, function (data) {
            $.each(data, function () {

                var new_div = $("<div>");

                var new_label = $("<label>");
                new_label.html(this.City);

                var new_input_b = $("<input>");
                new_input_b.attr("type", "button");

                new_div.append(new_label);
                new_div.append(new_input_b);

                $("#institutions").append(new_div);
            });
        });
    });

$("#institutions") is a div where i want to display all of the results. .post works correct for sure because certain institutions are retrieved from database, and passed to the view as Json result. But then I am affraid it wont itterate with .each.

Any help, coments or pointing in some direction would be much appriciated

+1  A: 

The above code will work if you set the dataType to JSON:

$(document).ready(function () {
        $.post("/Profile/Institutions",
        { id: <%= Model.Profile.userProfileID %> }, 
        function (data) {
            $.each(data, function () {

                var new_div = $("<div>");

                var new_label = $("<label>");
                new_label.html(this.City);

                var new_input_b = $("<input>");
                new_input_b.attr("type", "button");

                new_div.append(new_label);
                new_div.append(new_input_b);

                $("#institutions").append(new_div);
            });
        },
        "json");
    });
Craig Stuntz
+1 beat me to it
munch
A: 

Ty for quick reply. But affraid it doesnt work still. :( Seems like it wont generate even single new_div it prolly doesnt even iterate once.

Could there be some kind of parse issue with what controller returns? What fr.getInstitutions(id); returns is IQueryable. I tried with IEnumerable and making a list out of it but with no luck.

It could be. Since you don't show this, it's hard to be sure. Look at it.
Craig Stuntz