3

我正在尝试将我的模型发布回控制器,但 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 中的一个元素:

<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      

一些问题:

  1. 如何确保我在帖子中将 TeamModel 的 IEnumerable 返回到控制器?
  2. 我是否要以正确的方式将联盟传回给控制器?(通过隐藏字段)
4

1 回答 1

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>
}
于 2012-11-20T00:10:51.353 回答