我正在尝试将模型发布回控制器,但FormCollection中的结果并不是我所期望的。
我从自定义XML服务中收到了一个IEnumerable的TeamModel。每个TeamModel都包含一个字符串和一个bool。字符串是团队的名称,bool表示用户是否要将其插入数据库。
控制器:
public ActionResult ImportTeams()
{
var xmlService = new XmlSoccerService();
const string league = "Scottish Premier League";
var model = xmlService.GetAllTeamsByLeagueAndSeason(league, "1112");
ViewBag.League = league;
return View("~/Views/Admin/Database/ImportTeams.cshtml", model);
}
[HttpPost]
public ActionResult ImportTeams(FormCollection collection , string league)
{
return RedirectToAction("ImportTeams");
}
TeamModel:
public class TeamModel
{
public string Name { get; set; }
public bool Import { get; set; }
}
查看:
@model IEnumerable<TopTipFootball.XmlSoccer.Models.TeamModel>
@{
ViewBag.Title = "Import Teams";
}
<h2>Select the teams to import into: @ViewBag.League</h2>
@using (Html.BeginForm())
{
var league = ViewBag.League;
@Html.HiddenFor(l => league)
foreach (var team in Model)
{
<div class="editor-field">
@Html.EditorFor(model => team.Import)
@Html.DisplayFor(m => team.Name)
</div>
}
<p>
<input type="submit" value="Import teams" />
</p>
}
来自View的渲染html中的一个元素:
<input checked="checked" class="check-box" data-val="true" data-val-required="The Import field is required." id="team_Import" name="team.Import" type="checkbox" value="true" />
<input name="team.Import" type="hidden" value="false" />
Aberdeen
有些问题:
答案 0 :(得分:3)
尝试使用for循环进行循环,即
for(int i = 0; i < Model.Count; i++)
{
@Html.EditorFor(model => Model[i].Import)
@Html.DisplayFor(m => Model[i].Name)
}
我相信这应该创建id和名称,例如Model_ 0 _Import等,希望这会使它在你的帖子上正确绑定。
是的,我用这种方式使用了隐藏的字段。我当然假设联盟正在被正确发布,而这只是不是的项目列表?
编辑:您可以随时使用viewModel,而不是使用viewBag和Model的组合?
这是一个可能提供帮助的解决方案吗?
public ActionResult ImportTeams()
{
const string league = "Scottish Premier League";
var viewModel = new LeagueTeamViewModel
{
League = league;
}
var xmlService = new XmlSoccerService();
var model = xmlService.GetAllTeamsByLeagueAndSeason(league, "1112");
viewModel.Teams.AddRange(xmlService.GetAllTeamsByLeagueAndSeason(league, "1112").Select(p => new TeamViewModel
{
Name = p.Name,
Import = p.Import
};
return View("ImportTeams", viewModel);
}
[HttpPost]
public ActionResult ImportTeams(LeagueTeamViewModel viewModel)
{
}
public class LeagueTeamViewModel
{
public string League { get; set; }
private List<TeamViewModel> _teams = new List<TeamViewModel>();
public List<TeamViewModel> Teams
{
get { return _teams; }
set { _teams = value; }
}
}
public class TeamViewModel
{
[DisplayName("Name")]
public string Name { get; set; |
[DisplayName("Imported")]
public string Import { get; set; |
}
和你的观点
@model IEnumerable<LeagueTeamViewModel>
@{
ViewBag.Title = "Import Teams";
}
<h2>Select the teams to import into: @Model.League</h2>
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.League)
for(int i = 0; i < Model.Teams.Count; i++)
{
<div class="editor-field">
@Html.EditorFor(model => model.Teams[i].Import)
@Html.HiddenFor(m => model.Teams[i].Name)
</div>
}
<p>
<input type="submit" value="Import teams" />
</p>
}