1

I have the following structure:

  1. WebService - get the data from database and return the list of persons
  2. ASP.NET MVC application - uses web service from 1).

There's a method in HomeControler:

public JsonResult GetDbData()
{
    WebService1SoapClient client = new WebService1SoapClient();
    List<Person> lstPersons = client.GetPersons();

    return Json(lstPersons, JsonRequestBehavior.AllowGet);
}

I'd like to display the list of persons in Index.cshtml view using jQuery Ajax call in this manner:

<div>
    <div id="div3">Database data.</div>
</div>

<input id="btnGetDBData" type="button" value="GetDBData" />

$('#btnGetDBData').click(function () {
    $.ajax({
        url: "/Home/GetDbData",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $("#div3").html(data);
        },
        error: function (xhr, status) {
            alert(status);
        }
    });
});

But, it's not working.

I'm new in jQuery and need some kind of table or template or something like repeater that can display the list or table structure.

The Person class looks as following:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

I need to display the list of persons in Html table.

3
  • 2
    you are returning an array, you should try loop or access it using an array index => stackoverflow.com/questions/15848183/… Commented Jul 23, 2013 at 9:12
  • make a partial view, showing the list and return it from your action. Commented Jul 23, 2013 at 9:21
  • Everything should be in JavaScript (jQuery). Commented Jul 23, 2013 at 9:36

3 Answers 3

8

Why you are setting content of div3 with .html?

success: function (data) {
    $("#div3").html('');
    var div3Content = '';
    for(var i = 0; i < data.length; i++)
    {
       div3Content += '<p>' + data[i].Name + '</p>'; // if Name is property of your Person object
    }
    $("#div3").append(div3Content);
},

In this way, Person names will be added into div3 as <p> tag

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

6 Comments

@FSou1: typo mistake, thanks for pointing that out - corrected
$("#div3").html(''); will clear div in every iteration and only last data will be displayed
@Satpal: already updated. it'll clear div3 after each call to server, and than update the content as per the result
I don't really need div. It could be Html table.
@tesicg: in that case, instead of $("#div3").append('<p>'+data[i].Name+'</p>');, you'll just have to append row in table - $("#div3").append('<tr><td>'+data[i].Name+'</td></tr>');
|
2

In your data variable in success method you have an array of your data and you have to draw it manually

success: function (data) {
    $.each(data, function (index, value) {
        $("#div3").append(value.Id + '|' + value.FirstName + '|' + value.LastName);
    });
}

5 Comments

I know that and I ask for help how to do that.
You have data array in function, where each element is an object (isn't it?) So you could make it with jQuery
We don't how you would like to display it and what properties do you have
Yes. Please, take a look at the original post. There are required details.
For example, you could use it like this,- you have to decide your own output format
0

$('#div3').html(data) sets the HTML literally to what is found in data. However, after your jQuery call data contains an array of objects, not HTML. You first need to define what the HTML should look like. - Do you want it as a unordered list, a table, an array of nice-looking tiles, ...?

For rendering HTML from data I suggest you also consider templating and/or data-binding libraries such as, e.g. knockout which allows you to define the template in HTML:

<ul data-bind="foreach: persons">
    <li data-bind="text: LastName"></li>
</ul>

and your jQuery ajax success callback:

success: function (data) {
    ko.applyBindings({ persons: data });
}

The knockout foreach binding as above is kind of a repeater as you asked for.

Update: If you want a table the knockout template simply changes to something like:

<table>
    <thead><tr><th>First Name</th><th>Last Name</th><th>Full Name</th></tr></thead>
    <tbody data-bind="foreach: persons">
        <tr>
            <td data-bind="text: FirstName"></td>
            <td data-bind="text: LastName"></td>
            <td data-bind="text: FirstName() + ' ' + LastName()"></td>
        </tr>
    </tbody>
</table>

2 Comments

Yes. Please, take a look at the original post. There are required details.
@tesicg: I have updated my answer - you can use foreach like a repeater, i.e. you can do whatever you like with it, it does not need to be either of a table or a list. The snipped above only serves as an example.

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.