I am obviously doing something simply wrong, but I can't seem to figure it out. I am trying to pass my list of objects to my post action controller, but I am getting an Object reference not set to an instance of an object. error when I submit the form. I used fiddler to verify that the request does indeed contain the objects, but it does not make it into the post action.
My Model:
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; }
}
My View which displays fine:
@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"/>
}
and my post action which returns the error at the foreach statement:
[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");
}
For the model binding to occur properly, the name of the textboxes need to be in correct format.
I think below is what you are looking for: Modelbinding IEnumerable in ASP.NET MVC POST?
Ok here is the solution to my problem. ajp, you pointed me in the right direction. I had to return a List to the view instead of an IEnumerable so I could gain access to the index[]. Here is my updated code for the view:
@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"/>
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.