如何正确传递我的Ienumerable<>或列表<>反对我的帖子行动?

时间:2013-01-01 03:29:32

标签: c# asp.net-mvc razor

我显然做了一些错误的事情,但我似乎无法弄明白。我试图将我的对象列表传递给我的post动作控制器,但是当我提交表单时,我得到一个对象引用没有设置为对象的实例。错误。我使用fiddler验证请求确实包含了对象,但它没有进入post操作。

我的模特:

public class MakeNbaPickVm
{

    public List<NbaGamesForPicksVm> NbaGames { get; set; }


    public pick_UserPicks Pick { get; set; }


    public bool PickSpreadIsFavAway { get; set; }
    public bool PickSpreadIsFavHome { get; set; }
    public bool PickSpreadIsDogAway { get; set; }
    public bool PickSpreadIsDogHome { get; set; }
    public bool OUpickIsOVer { get; set; }
    public bool OUpickIsUnder { get; set; }
    public decimal LineOdds { get; set; }
    public decimal OuOdds { get; set; }
    public decimal LineBet { get; set; }
    public decimal OuBet { get; set; }
    public int MatchId { get; set; }
}

我的视图显示正常:

@model IEnumerable<MakeNbaPickVm>

@{
ViewBag.Title = "Make Nba Picks";
}

<h2>Make Nba Picks</h2>

@using(Html.BeginForm("MakeNbaPick", "Picks", FormMethod.Post))
{
<table class="PickTable">
    <tr>
        <th>Game Date</th>
        <th>Away</th>
        <th>Away Line</th>
        <th>Over/Under</th>
        <th>Home</th>
        <th>Home Line</th>
        <th>O/U Odds</th>
        <th>O/U Bet</th>
        <th>Line Odds</th>
        <th>Line Bet</th>
    </tr>
    @{
        int modLevel = 0;
    }
    @foreach(var game in Model)
    {
        foreach (var match in game.NbaGames)
        {
            @Html.HiddenFor(x => x.ElementAt(modLevel).MatchId, new {@Value = 
       match.Game.Id})
            <tr>
                <td>@match.Game.GameDateTime.Value.ToShortDateString() 
                    @match.Game.GameDateTime.Value.ToShortTimeString()
                </td>
                <td>@match.Away</td>
                @if(match.Game.Away == match.Game.FavoriteTeam)
                {
                    <td>
                        @match.Game.FavoriteSpread<br/> @Html.CheckBoxFor(x => 
                                                                          x.ElementAt(modLevel)
                                                                          .PickSpreadIsFavAway)
                    </td>
                }
                else
                {
                    <td>
                        @match.Game.UnderDogSpread<br/> @Html.CheckBoxFor(x => 
                                                                          x.ElementAt(modLevel)
                                                                          .PickSpreadIsDogAway)
                    </td>
                }
                <td>
                    @match.Game.OverUnder<br/> Over @Html.CheckBoxFor(x => 
                                                                      x.ElementAt(modLevel)
                                                                          .OUpickIsOVer)<br/>
                    Under @Html.CheckBoxFor(x => x.ElementAt(modLevel)
                                                     .OUpickIsUnder)
                </td>
                <td>@match.Home</td>
                @if(match.Game.Home == match.Game.FavoriteTeam)
                {
                    <td>
                        @match.Game.FavoriteSpread<br/> @Html.CheckBoxFor(x => 


                                                  x.ElementAt(modLevel).PickSpreadIsFavHome)
                    </td>
                }
                else
                {
                    <td>
                        @match.Game.UnderDogSpread<br/> @Html.CheckBoxFor(x => 
                                                                          x.ElementAt(modLevel)

                                                                         .PickSpreadIsDogHome)
                    </td>
                }
                <td>@Html.TextBoxFor(x => x.ElementAt(modLevel).OuOdds, 
                                     new {@class="PickOddsBet"})</td>
                <td>@Html.TextBoxFor(x => x.ElementAt(modLevel).OuBet,
                                     new {@class="PickOddsBet"})</td>
                <td>@Html.TextBoxFor(x => x.ElementAt(modLevel).LineOdds,
                                     new {@class="PickOddsBet"})</td>
                <td>@Html.TextBoxFor(x => x.ElementAt(modLevel).LineBet,
                                     new {@class="PickOddsBet"})</td>
            </tr>
            modLevel++;
        }

    }
</table>

<input type="submit"/>
}

以及我在postach语句中返回错误的帖子操作:

[HttpPost]
    public ActionResult MakeNbaPick(List<MakeNbaPickVm> vM)
    {
        // Save the picks to db
        foreach (var pickVm in vM)
        {
           // code doesn't make it this far
        }

        return RedirectToAction("Games");
    }

2 个答案:

答案 0 :(得分:1)

要正确进行模型绑定,文本框的名称必须格式正确。

我认为以下是您正在寻找的: Modelbinding IEnumerable in ASP.NET MVC POST?

答案 1 :(得分:0)

好的,这是我的问题的解决方案。 ajp,你指出我正确的方向。我不得不将List返回到视图而不是IEnumerable,因此我可以访问索引[]。以下是我更新的视图代码:

@model List<MakeNbaPickVm>

@{
ViewBag.Title = "Make Nba Picks";
}

<h2>Make Nba Picks</h2>

@using(Html.BeginForm("MakeNbaPick", "Picks", FormMethod.Post))
{
<table class="PickTable">
    <tr>
        <th>Game Date</th>
        <th>Away</th>
        <th>Away Line</th>
        <th>Over/Under</th>
        <th>Home</th>
        <th>Home Line</th>
        <th>O/U Odds</th>
        <th>O/U Bet</th>
        <th>Line Odds</th>
        <th>Line Bet</th>
    </tr>
    @{
        int modLevel = 0;
    }
    @foreach(var game in Model)
    {
        foreach (var match in game.NbaGames)
        {
            @Html.HiddenFor(x => x[modLevel].MatchId, new {@Value = 
       match.Game.Id})
            <tr>
                <td>@match.Game.GameDateTime.Value.ToShortDateString() 
                    @match.Game.GameDateTime.Value.ToShortTimeString()
                </td>
                <td>@match.Away</td>
                @if(match.Game.Away == match.Game.FavoriteTeam)
                {
                    <td>
                        @match.Game.FavoriteSpread<br/> @Html.CheckBoxFor(x => 
                                                                          x[modLevel]
                                                                          .PickSpreadIsFavAway)
                    </td>
                }
                else
                {
                    <td>
                        @match.Game.UnderDogSpread<br/> @Html.CheckBoxFor(x => 
                                                                          x[modLevel]
                                                                      .PickSpreadIsDogAway)
                    </td>
                }
                <td>
                    @match.Game.OverUnder<br/> Over @Html.CheckBoxFor(x => 
                                                                      x[modLevel]
                                                                          .OUpickIsOVer)<br/>
                    Under @Html.CheckBoxFor(x => x[modLevel]
                                                     .OUpickIsUnder)
                </td>
                <td>@match.Home</td>
                @if(match.Game.Home == match.Game.FavoriteTeam)
                {
                    <td>
                        @match.Game.FavoriteSpread<br/> @Html.CheckBoxFor(x => 
                                                                x[modLevel].PickSpreadIsFavHome)
                    </td>
                }
                else
                {
                    <td>
                        @match.Game.UnderDogSpread<br/> @Html.CheckBoxFor(x => 
                                                                          x[modLevel]
                                                                       .PickSpreadIsDogHome)
                    </td>
                }
                <td>@Html.TextBoxFor(x => x[modLevel].OuOdds, 
                                     new {@class="PickOddsBet"})</td>
                <td>@Html.TextBoxFor(x => x[modLevel].OuBet,
                                     new {@class="PickOddsBet"})</td>
                <td>@Html.TextBoxFor(x => x[modLevel].LineOdds,
                                     new {@class="PickOddsBet"})</td>
                <td>@Html.TextBoxFor(x => x[modLevel].LineBet,
                                     new {@class="PickOddsBet"})</td>
            </tr>
            modLevel++;
        }

    }
</table>

<input type="submit"/>
}