ChrisO
ChrisO

Reputation: 5465

MVC 3 form postback with model

I am trying to post my model back to the controller but the results in the FormCollection are not what I was expecting.

I receive an IEnumerable of TeamModel back from my custom XML service. Each TeamModel contains a string and a bool. The string is the name of the team and the bool signifies whether the user wants to insert it into the database.

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");
    }

TeamModel:

public class TeamModel
{
    public string Name { get; set; }
    public bool Import { get; set; }
}

View:

@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>
}

One element from the View's rendered 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      

Some questions:

  1. How can I ensure that I get back an IEnumerable of TeamModel in the post back to the controller?
  2. Am I going about passing the league back to the controller in the correct way? (Through a hidden field)

Upvotes: 3

Views: 2387

Answers (1)

dreza
dreza

Reputation: 3645

Try looping using a for loop instead i.e.

for(int i = 0; i < Model.Count; i++) 
{
   @Html.EditorFor(model => Model[i].Import)
   @Html.DisplayFor(m => Model[i].Name)  
}

I believe this should create id's and names such as Model_0_Import etc and hopefully this will make it bind correctly on your post.

And yes, I've used hidden fields in this way. I assume of course the league is being posted correctly and it's just the list of items that aren't?

EDIT: You could always go down the track of using a viewModel rather than using a combination of viewBag and Model?

Here's one solution that might offer some help?

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; |
}

And your view

@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>
}

Upvotes: 3

Related Questions