Javascript 如何填写<;ul>;使用jQuery和JSON ActionResult返回的列表?

Javascript 如何填写<;ul>;使用jQuery和JSON ActionResult返回的列表?,javascript,jquery,json,ajax,view,Javascript,Jquery,Json,Ajax,View,我可以在运行submit to Controller的视图中填充此内容 <h4>Stores In Area</h4> <ul> @foreach (var account in Model) { <li> @Html.ActionLink(account.UserName, "StoreAccountDetails", new { id = account.AccountID })

我可以在运行submit to Controller的视图中填充此内容

<h4>Stores In Area</h4>
<ul>
    @foreach (var account in Model)
    {

        <li>
            @Html.ActionLink(account.UserName, "StoreAccountDetails", new { id = account.AccountID })
            @Html.Encode(account.Phone)
            @Html.Encode(account.Email)
            @Html.Encode(account.Radius)
            @Html.Encode(account.Distance)
            @Html.Encode(account.City)
            @Html.Encode(account.State)
          </li>

    }
</ul>
选项1:您可以迭代
JSON
并填充表中的各个元素,如下所示:

  $.getJSON(url, function(data) {
    $.each(data, function(key, val) {
      var row= '<tr><td>' + val.your_property + '</td></tr>';
      $('#table').append(row);
    });
  });
函数handleResultResponse(responseObject){
var_liHTML=“”,
_ul=$(“ul”);//应提供id
$.each(响应对象、函数(索引、值){
_liHTML+=“
  • ”+value.property_name+”
  • ”; }); _ul.html(_liHTML); }
    在尝试了对我的问题的两种答案并进行了深入研究之后,我发现让我的代码尽快运行的唯一方法是在控制器的JSONResult中编写一个表,并将其作为字符串发送,然后用它设置Div的innerHtml

    在即时窗口中,我能够找出从存储过程返回的list()的内容。我能够循环并填充一个html表

    我会将我的成功归因于这样一个事实:我可以设置一个断点,然后对我发送回来的列表对象提出问题。我能够获得行数,以及每个字段的语法:stores[I].AccountID。商店,计数等等

    对于在视图响应中返回的JSON对象,我无法理解这一点,我也无法在运行时在视图中的JavaScript代码中停止。我能够使用JSON.stringify(value)获取整行: (我知道里面有东西,我就是看不到JSON jQuery块中循环的loopin来搞乱它……如果我能弄清楚语法的话

    以下是stringify为我的responseObject显示的内容:

    {"AccountID":2,"UserName":"User1","Phone":"503-358-5901","Email":"xxx@xxx.com","‌​Radius":80,"Distance":1.7788856195409057,"City":null,"State":null,"Lat":45.52,"Lo‌​ng":-122.64} {"AccountID":3,"UserName":"FredAckStare","Phone":"5039999999","Email":"www@sss.c‌​om","Radius":100,"Distance":1.9836792859217536,"City":null,"State":null,"Lat":45.‌​51,"Long":-122.64} – and so on.
    
    我对HTML表并用“编辑”“删除”列调整它们并不陌生,现在一切都有可能了。我想这只是简单的脚本编写,但我不打算返回很多行,甚至不认为JSON对象比字符串快

    但主要来说,这个答案更多的是解决我的问题,问题概述了这个问题,尽管有人可以解释如何在一个循环中循环,以从返回的JSON响应/列表中获得每行8-10列。我猜是一个聪明的人在忙着工作,没有阅读我的问题。哈哈

    我不想打乱众神,所以如果这个答案不合适,请给出建议。如果你愿意,也可以发布一个答案

    以下是我的控制器的html字符串脚本代码:

    public JsonResult GetStores(float lat, float lng)
    {
    
        var stores = _db.GetDistanceFromLatLongs2(lat, lng).ToList();
    
        var results = new JsonResult();
    
        if (stores.Any())
        {
            string htmlString = "<table><thead><th>AccountID</th><th>UserName</th><th>Phone</th></thead>";
            var i = 0;
                while(i < stores.Count)
                {
                    htmlString += "<tr>";
                    htmlString += "<td>" + stores[i].AccountID + "</td>";
                    htmlString += "<td>" + stores[i].UserName + "</td>";
                    htmlString += "<td>" + stores[i].Phone + "</td>";
                    htmlString += "</tr>";
                    i++;
                }
    
            htmlString += "</table>";
    
            results = new JsonResult { Data = htmlString};
        }
    
        return results;
        }
    }
    
    public JsonResult GetStores(浮动lat、浮动lng)
    {
    var stores=_db.GetDistanceFromLatLongs2(lat,lng).ToList();
    var results=新的JsonResult();
    if(stores.Any())
    {
    字符串htmlString=“AccountIDUserNamePhone”;
    var i=0;
    而(i
    value.property\u name返回“undefined”。如果我在2“li”中使用JSON.stringify(value),我会得到我想要的整行,但它有字段名,除了值之外还有引号:{“AccountID”:2,“UserName”:“User1”,“Phone”:“503-358-5901”,“Email”:xxx@xxx.com,“半径”:80,“距离”:1.7788856195409057,“城市”:空,“州”:空,“纬度”:45.52,“长”:-122.64}{“AccountID”:3,“用户名”:“FredAckStare”,“电话”:“503999999”,“电子邮件”:www@sss.com“,“半径”:100,“距离”:1.9836792859217536,“城市”:空,“州”:空,“纬度”:45.51,“长”:-122.64}”
    <tr ng-repeat="item in items">
      <td ng-repeat="(key, val) in item">
        {{key}}: {{val}}
      </td >
    </tr >
    
    function handleResultResponse(responseObject) {
        var _liHTML = "",
            _ul = $("ul"); //should provide id
    
        $.each(responseObject, function(index, value){
            _liHTML += "<li>" + value.property_name + "</li>";
        });
    
        _ul.html(_liHTML);
    }
    
    {"AccountID":2,"UserName":"User1","Phone":"503-358-5901","Email":"xxx@xxx.com","‌​Radius":80,"Distance":1.7788856195409057,"City":null,"State":null,"Lat":45.52,"Lo‌​ng":-122.64} {"AccountID":3,"UserName":"FredAckStare","Phone":"5039999999","Email":"www@sss.c‌​om","Radius":100,"Distance":1.9836792859217536,"City":null,"State":null,"Lat":45.‌​51,"Long":-122.64} – and so on.
    
    public JsonResult GetStores(float lat, float lng)
    {
    
        var stores = _db.GetDistanceFromLatLongs2(lat, lng).ToList();
    
        var results = new JsonResult();
    
        if (stores.Any())
        {
            string htmlString = "<table><thead><th>AccountID</th><th>UserName</th><th>Phone</th></thead>";
            var i = 0;
                while(i < stores.Count)
                {
                    htmlString += "<tr>";
                    htmlString += "<td>" + stores[i].AccountID + "</td>";
                    htmlString += "<td>" + stores[i].UserName + "</td>";
                    htmlString += "<td>" + stores[i].Phone + "</td>";
                    htmlString += "</tr>";
                    i++;
                }
    
            htmlString += "</table>";
    
            results = new JsonResult { Data = htmlString};
        }
    
        return results;
        }
    }