在foreach循环MVC之后查看不渲染

时间:2014-06-26 18:49:18

标签: ajax asp.net-mvc razor

目标:我正在尝试使用Razor语法填充表格,根据下拉列表填充表格。

总结:我将模型传递给我的View并通过模型中的每个对象循环。调试View时,我能够看到模型中填充的对象,但是,当页面实际显示时,表格中没有任何内容,唯一显示的是下拉列表。

问题:页面呈现可能出现什么问题?

我的观点如下:

    @model IEnumerable<FantasySportsMVC.Models.PlayerDetails>

    @{
        ViewBag.Title = "Position";
    }

    <h2>Position</h2>

    <body>
        <div>
            @Html.DropDownList("ddlTournaments",(IEnumerable<SelectListItem>)ViewBag.Tournaments, new { id="ddlTournament", name="ddlTournament"})
        </div>
        <div>
            <input type="button" id="btnGetData" value="Show me some stuff, Yo!" />
        </div>
        <div id="results">
        </div>
        <table id="tbDetails">
            @if(Model != null)
            {
                <tbody>
                    @foreach (var player in Model)
                    {
                        <tr>
                            <td>@player.LastName</td>
                            <td>@player.FirstName</td>
                            <td>@player.Position</td>
                        </tr>
                    }
                </tbody>
            }
        </table>
    </body>

    <script type="text/javascript">
    function SendTournamentId() {
        var data = JSON.stringify({ id : $("#ddlTournament option:selected").val()});
        $.ajax({

            url: '/Leaderboard/Position',
            type: 'POST',
            dataType: 'json',
            data: data,
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                //return JSON.stringify($("#ddlTournament option:selected").val());
                $("#ddlTournament option:selected").val(result.d.id);
            }
        });
    }

    $(function () {
        $('#btnGetData').click(SendTournamentId);
    });

</script>

我的控制器如下:

    public class LeaderboardController : Controller
        {
        public ActionResult Position()
            {
                ViewBag.Tournaments = GetTournamentDetailsSelectList();
                return View();
            }

            [HttpPost]
            public ActionResult Position(string id)
            {
                ViewBag.Tournaments = GetTournamentDetailsSelectList();
                var tournamentId = id;
                var url = ConstructLeaderboardUrl(tournamentId);
                var xmlToJsonUrl = ConvertXmltoJson(url);
                List<PlayerDetails> details = BindDataTablePlayerDetails(xmlToJsonUrl);
                return View(details);
            }
        }

private static List<PlayerDetails> BindDataTablePlayerDetails(string url)
{
    dtAttributeList = new DataTable();
    var details = new List<PlayerDetails>();

    try
    {
        //ConvertXmltoJson(url);

        // Construct Datatable
        dtAttributeList.Columns.Add("Last Name", typeof(string));
        dtAttributeList.Columns.Add("First Name", typeof(string));
        dtAttributeList.Columns.Add("Position", typeof(string));

        // Add rows to Datatable from Json
        for (int i = 0; i < doc.GetElementsByTagName("player").Count; i++)
        {
            dtAttributeList.Rows.Add(
                doc.GetElementsByTagName("player").Item(i).Attributes["last_name"].Value,
                doc.GetElementsByTagName("player").Item(i).Attributes["first_name"].Value,
                doc.GetElementsByTagName("player").Item(i).Attributes["position"].Value);

        }

        // Add rows from Datatable to PlayerDetails
        foreach (DataRow row in dtAttributeList.Rows)
        {
            var player = new PlayerDetails();
            player.LastName = row["Last Name"].ToString();
            player.FirstName = row["First Name"].ToString();
            player.Position = row["Position"].ToString();
            details.Add(player);
        }
    }
    catch (Exception e)
    {
        throw new Exception();
    }

    return details;

}

0 个答案:

没有答案