0

Wonder if someone could point me in the right direction please?

Basically I'm trying to create a page to allow someone to enter data (golf) on a number (18) of holes and basically I'm unsure of how it's done.

In the model I've created the following:

namespace BGSociety.Models
{
public class CreateCourseHolesViewModel
{
    public int holeNumber { get; set; }
    public int par { get; set; }
    public int si { get; set; }
    public int distance { get; set; }
}

}

In the Index event of the controller I've got:

TempData["holes"] = getHoles();

    protected List<CreateCourseHolesViewModel> getHoles()
    {
        var holes = new List<CreateCourseHolesViewModel>();

        for (int i =1; i < 19; i++)
        {
            holes.Add(new CreateCourseHolesViewModel { holeNumber = i });
        }
        return holes;

    }

And I'm passing this into the view:

return PartialView("CreateCourseHoles", TempData["holes"]);

In the view I can loop through the list and display the whole number with a textbox next to each one to allow for the entry of par, si and distance.

    var zHoles = TempData["holes"] as IEnumerable<BGSociety.Models.CreateCourseHolesViewModel>;
    foreach (var hole in zHoles)
    {
        <div class="row">
            <div class="col-sm-1">
                <p>@hole.holeNumber</p>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <div class="col-xs-12">
                        @Html.ValidationMessageFor(m => hole.par)
                        @Html.TextBox("par", TempData["par"], new { @class = "form-control", placeholder = "Par", name = "Par" })
                    </div>
                </div> 
            </div>
        </div>
    }

But I just can't work out how to pass this populated list back to the controller for me to enter the data into the DB.

There's a great chance that I've gone about this the wrong way (!) but if someone could spare a few minutes to assist that would be marvelous!

Thanks, Sx

tereško
  • 58,060
  • 25
  • 98
  • 150
SxChoc
  • 619
  • 3
  • 10
  • 26
  • Firstly you cannot use a `foreach` loop to generate form controls for collection items (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for an explanation). But not a lot else you doing makes any sense. And do not use `TempData`, In you get method, you initialize your collection and pass it to the view. –  Mar 03 '16 at 04:01

1 Answers1

0

TempData is not meant to be passed as parameters to your PartialView. Even if you dont pass it as parameter; you can still access it. I suggest that you do this instead:

return PartialView("CreateCourseHoles", getHoles());

then your partial view you can you have

@model IEnumerable<BGSociety.Models.CreateCourseHolesViewModel>

and wrap all that for loop in a form:

 <form method="post" action="wherever you want to post it to">

foreach (var hole in Model)
    {
        <div class="row">
            <div class="col-sm-1">
                <p>@hole.holeNumber</p>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <div class="col-xs-12">
                        @Html.ValidationMessageFor(m => hole.par)
                        @Html.TextBox("par", TempData["par"], new { @class = "form-control", placeholder = "Par", name = "Par" })
                    </div>
                </div> 
            </div>
        </div>
    }

</form>
Golois Mouelet
  • 307
  • 2
  • 10
  • Cheers for that bud but I still can't see the 18 sets of data in the model I can only see the first one. When it's posted back to the controller I mean – SxChoc Mar 02 '16 at 22:30