2013-02-11 68 views
0

我试图在文本框中键入国家/地区名称并单击按钮时获取客户(姓名和地址)列表。使用getJSON在MVC中显示客户名称(姓名,地址)

这里是视图:

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

我的问题是:

我)当我使用以下

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

它工作正常,当我使用“字符串ID”但是当我使用“字符串国家”,然后“其中r.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); 

赞赏任何帮助的方式。

+0

尝试input'这里'$ .getJSON(URL,{数据:国家},功能(数据) {@ – Jai 2013-02-11 10:24:29

+0

i)@Jai没有任何反应。 – 2013-02-11 10:48:58

回答

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'); 
}); 

这里是放置`data`了,而不是`的文档http://api.jquery.com/jQuery.getJSON/

相关问题