0

テキストボックスに国名を入力してボタンをクリックすると、顧客のリスト(名前と住所)を取得しようとしています。

ビューは次のとおりです。

<p>
    Enter country name @Html.TextBox("Country")
    <input type="submit" id="GetCustomers" value="Submit"/>
</p>

JSON呼び出しは次のとおりです。

<script type="text/jscript">
    $('#GetCustomers').click(function () {

        //var url = "/Home/CustomerList";
        //var Country = $('#Country').val();
        //$.getJSON(url, { input: Country }, function (data) {

        $.getJSON('/Home/CustomerList/' + $('#Country').val(), function (data) {

            var items = '<table><tr><th>Name</th><th>Address</th></tr>';
            $.each(data, function (i, country) {
                items += "<tr><td>" + country.ContactName + "</td><td>" + country.Address + "</td></tr>";
            });
            items += "</table>";

            $('#rData').html(items);
        });
    })
</script>

コントローラは次のとおりです。

public JsonResult CustomerList(string Id)
{
    var result = from r in db.Customers
                    where r.Country == Id
                    select r;
    return Json(result);
}

私の問題は次のとおりです。

i)以下を使用している場合

var url = "/Home/CustomerList";

var Country = $('#Country').val();

$.getJSON(url, { input: Country }, function (data) {

prameterをCustomerListメソッドに渡していませんが、以下は正常に機能します

$.getJSON('/Home/CustomerList/' + $('#Country').val(), function (data) {

ii)次のJSONを使用している場合

$.getJSON('/Home/CustomerList/' + $('#Country').val(), function (data) {

次にCustomerListメソッドに従います

public JsonResult CustomerList(string Id)
{
    var result = from r in db.Customers
                    where r.Country == Id
                    select r;
    return Json(result);
}

'string Id'を使用すると正常に機能しますが、'stringcountry'を使用してから'where r.Country == country'を使用すると、機能しません。

iii)これは、機能するのではなく、応答を処理する正しい方法ですか?

var items = '<table><tr><th>Name</th><th>Address</th></tr>';
$.each(data, function (i, country) {
    items += "<tr><td>" + country.ContactName + "</td><td>" + country.Address + "</td></tr>";
});
items += "</table>";

$('#rData').html(items);

助けていただければ幸いです。

4

1 に答える 1

1

これを試して

 $('#GetCustomers').click(function () {

    //var url = "/Home/CustomerList";
    //var Country = $('#Country').val();
    //$.getJSON(url, { input: Country }, function (data) {

    $.getJSON('/Home/CustomerList/' + $('#Country').val(), function (data) {

        var items = '<table><tr><th>Name</th><th>Address</th></tr>';
        $.each(data, function (i, country) {
            items += "<tr><td>" + country.ContactName + "</td><td>" + country.Address + "</td></tr>";
        });
        items += "</table>";

        $('#rData').html(items);
    },'json');
});

これがドキュメントですhttp://api.jquery.com/jQuery.getJSON/

于 2013-02-11T10:25:02.113 に答える