作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将我的模型发布回 Controller ,但 FormCollection 中的结果不是我所期望的。
我收到了从我的自定义 XML 服务返回的 TeamModel 的 IEnumerable。每个 TeamModel 都包含一个字符串和一个 bool 值。字符串是团队的名称, bool 表示用户是否要将其插入数据库。
Controller :
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");
}
团队模型:
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
一些问题:
最佳答案
尝试使用 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 等,希望这将使它在您的帖子上正确绑定(bind)。
是的,我以这种方式使用了隐藏字段。我当然假设联赛发布正确,只是项目列表不正确?
编辑:您总是可以继续使用 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>
}
关于asp.net-mvc - 带有模型的 MVC 3 表单回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13464581/
我是一名优秀的程序员,十分优秀!