在MVC中从Jquery将数据加载到表中

时间:2012-09-12 08:39:34

标签: jquery json asp.net-mvc-3 razor

我从控制器返回了这个Json:

 public ActionResult VerifyApp(string recurrence)
        {
            List<foo> item = schedulingProxy.CheckApp(recurrence);

            return Json(item, JsonRequestBehavior.AllowGet);
        }

我认为我有这张表:

<div class="table">
                <table id="thids" class="bg">
                    <thead>
                        <tr>
                            <th>
                                Date
                            </th>
                            <th>
                                Time
                            </th>
                            <th>
                                Slot
                            </th>
                            <th>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in conflictList)
                        {
                            <tr id="@item.ID">
                                <td>   
                                  @Html.DisplayFor(modelItem => item.ResourceFName)                             
                                </td>
                                <td>
                                  @Html.DisplayFor(modelItem => item.ResourceLName)
                                </td>
                                <td>
                                  @Html.DisplayFor(modelItem => item.ResourceSlot)
                                </td>
                                <td>
                                  @Html.DisplayFor(modelItem => item.reason)
                                </td>
                            </tr>                                 
                        }
                    </tbody>
                </table> 

我也在视图的顶部添加了这个:

@{
    var conflictList =new List<Mavi.Domain.Model.contactThread>();
}

我在我的Jquery中有这个Axaj调用:

function ChkAvailable() {

        pairString = pairString + newObj.ToString() + "]";
        var requrl = '@Url.Action("VerifyAppointment", "Scheduling", null, Request.Url.Scheme, null)';
        $.ajax({
            type: "POST",
            url: requrl,
            data: { recurrence: pairString },
            success: function (data) {
               //returned List<foo>
            }
        });
    }

如何将我返回的JSon对象中的值添加到conflictList中?或者更好..我怎样才能填充我的表格?

1 个答案:

答案 0 :(得分:4)

如果我理解你的话,你只需要将新的html内容附加到你的表中,如下所示:

...
success: function (data) {
   for(var el in data)
   {
     $("#thids").find("tbody")
        .append($("<tr>").append($("<td>").text(el.ResourceFName))/*and so on*/);
   }               
}
...