モデルをコントローラーにポストしようとしていますが、FormCollection の結果は期待したものではありません。
カスタム XML サービスから TeamModel の IEnumerable を受け取りました。各 TeamModel には文字列とブール値が含まれています。文字列はチームの名前で、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");
}
チームモデル:
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>
}
ビューのレンダリングされた html からの 1 つの要素:
<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
いくつかの質問:
- コントローラーへのポストで TeamModel の IEnumerable を確実に取得するにはどうすればよいですか?
- 正しい方法でリーグをコントローラーに戻そうとしていますか? (隠しフィールドを通して)