Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:40:19+00:00 2026-06-14T14:40:19+00:00

I am trying to post my model back to the controller but the results

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:40:21+00:00Added an answer on June 14, 2026 at 2:40 pm

    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>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to post JSON data to Spring controller.. Not working at all JSP Code:
Im having issues when trying to post a IList back to the controller here
Trying to set-up a call back function, but it's not working for some reason.
I am trying to do some jquery.post where I pass back the Model. I
I'm trying to add comments to a Post model class Comment < ActiveRecord::Base belongs_to
I am trying to setup a model where a user can post an advertisement
Trying to post a screenshot in a tweet from within my app.. so far
I'm trying to POST a PayPal or GoogleCheckout buy now button from the code
I am trying to get my view to post a List back to the
I trying to return the same model back to the view that edited the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.