0

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 Answer 1

1

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