繁体   English   中英

如何正确地将Ienumerable <>或List <>对象传递给我的发布操作?

[英]How do I correctly pass my Ienumerable<> or List<> object to my post action?

我显然做错了什么,但我似乎无法弄清楚。 我试图将对象列表传递给我的后动作控制器,但是我得到的对象引用未设置为对象的实例。 我提交表单时出错。 我使用了提琴手来验证请求是否确实包含对象,但没有使它进入后期操作。

我的模特:

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"/>
}

我的post操作在foreach语句中返回错误:

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

为了使模型绑定正确进行,文本框的名称必须采用正确的格式。

我认为以下是您要查找的内容: 在ASP.NET MVC POST中进行模型绑定IEnumerable?

好的,这是我的问题的解决方案。 ajp,您为我指明了正确的方向。 我必须将List而不是IEnumerable返回到视图,以便可以访问index []。 这是我对该视图的更新代码:

@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"/>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM