我的模特。
public class CustomUserTeam
{
public bool IsCation { get; set; }
public int SelectedPlayer { get; set; }
public IEnumerable<SelectListItem> Player { get; set; }
}
我的创建视图
@model List<superselector.Models.CustomUserTeam>
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "UserPlayers", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Team</h4>
<hr />
@Html.ValidationSummary(true)
<table>
<tr>
<th>
Player
</th>
<th>
Caption
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<div class="form-group">
<div class="col-md-10">
@Html.DropDownListFor(model => item.SelectedPlayer, item.Player)
@Html.ValidationMessageFor(model => item.SelectedPlayer)
</div>
</div>
</td>
<td>
<div class="form-group">
<div class="col-md-10">
@Html.RadioButtonFor(model => item.IsCation, true)
@Html.ValidationMessageFor(model => item.IsCation)
</div>
</div>
</td>
</tr>
}
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我的创建动作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(List<CustomUserTeam> utvm)// I get null here
{
if (ModelState.IsValid)
{
//db.tblUserTeams.Add(tbluserteam);
//db.SaveChanges();
return RedirectToAction("Index");
}
return View(utvm);
}
public ActionResult Create()
{
//UserTeamViewModel utvm = new UserTeamViewModel();
List<CustomUserTeam> UserTeam = new List<CustomUserTeam>();
List<tblPlayer> batsmen = db.tblPlayers.Where(x => x.tblPlayerSpeciality.Id == 1 || x.tblPlayerSpeciality.Id == 3).OrderBy(x => x.PlayerName).ToList();
List<tblPlayer> Bowlers = db.tblPlayers.Where(x => x.Speciality == 2 || x.Speciality == 3).OrderBy(x => x.PlayerName).ToList();
List<tblPlayer> wk = db.tblPlayers.Where(x => x.Speciality == 4).OrderBy(x => x.PlayerName).ToList();
for (int i = 0; i < 6; i++)
{
UserTeam.Add(new CustomUserTeam() { Player = new SelectList(batsmen, "playerId", "PlayerName") });
}
UserTeam.Add(new CustomUserTeam() { Player = new SelectList(wk, "playerId", "PlayerName") });
for (int i = 0; i < 4; i++)
{
UserTeam.Add(new CustomUserTeam() { Player = new SelectList(Bowlers, "playerId", "PlayerName") });
}
return View(UserTeam);
}
问题是当我点击View上的“Create”时,我在Create Action的参数中得到null
我不知道什么是错的