-1

I am outputting data that I get from a query and placing an input box by each one on the view.

This is a partial view inside one of my forms.

@model IEnumerable<Project.Models.VehicleTypeSeat>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
    int counter = 0;
}
<p>

</p>


    <table>
        <tr>
            <th>
                <div class="editor-field">
                    Crew
                </div>
            </th>
            <th>
                Description
            </th>

        </tr>
@{ 
    @for (var item in model)
    {
            <tr>
                <input type="hidden" name="raceSeats.index" value="@(counter)" />
                <td>
                    @Html.DropDownList(item.CrewID, null, "--Select Crew Member--", htmlAttributes: new { @class = "form-control", @name = "raceSeats["+@counter+"].PersonID" })
                </td>

                <td>
    @Html.DisplayFor(modelItem => item.VehicleTypeSeatDescription)
</td>



            </tr>
            counter += 1;
        }

    </table>
}

I am trying to accomplish this kind of thing to read as an array by the post:

<input type="hidden" name="people.index" value="0" />
<input type="text" name="people[0].firstname" />
<input type="text" name="people[0].lastname" />

The problem is that I am having trouble manipulating the names of "DisplayFor" and "DropDownList"

Travis Tubbs
  • 827
  • 1
  • 14
  • 32
  • You should handle that logic in your controller. In your controller assign the data list to a (for instance) ViewBag.Seats and in your View use a normal structure for displaying a single model item that uses dropdownfor(that item) and make the ViewBag.Seats the source for it. – nocturns2 Nov 17 '16 at 14:51
  • Yeah, the dropdownlist is created in the controller. It is ViewBag.CrewID. The problem however is that when I post this code above, I look for VehicleAndCrew[] vehicleAndCrew. I cant create a dynamic number of viewBag items because the view wont know exactly how many there are. – Travis Tubbs Nov 17 '16 at 15:01
  • I'm still not understanding your intensions, but if you've created the list or array in your controller, you can always pass the list count (or array.length) via (say) ViewBag.CrewCount. – nocturns2 Nov 17 '16 at 15:16
  • It's hard to see what you're trying to achieve from the information given. What ViewModel classes are you using for the main, and partial views? – John M Nov 17 '16 at 15:23
  • Okay, I updated the question to show the full partial view. Heres exactly what I am doing. On the main view, I have a form which creates a race (as in vehicle race). The purpose of this view is that the first thing a user does is to select a vehicle to race in, and then that populates a box which will provide a dropdown box and a description of what seat that is(this is where I am struggling because I need an array to bind). Then When the user submits the form, it will post two objects, a Race object and a RaceSeats[] array. and on the controller I will bind them. – Travis Tubbs Nov 17 '16 at 15:31
  • I am ultimately trying to get an array of raceSeats that I can add to the Race on the controller. – Travis Tubbs Nov 17 '16 at 16:39
  • You have not shown the relevant view code, but if you dynamically wanting to add (and delete) collection items, refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options –  Nov 18 '16 at 00:11

1 Answers1

0

I'm not sure I fully understand the question, but I have found that when you want to bind back to a model foreach can cause problems. Instead try

@for (int i = 0; i < model.Count(); i++)
{
    // reference each item by index
    @Html.DropDownListFor(model[i].CrewID, 
       null, "--Select Crew Member--", htmlAttributes: new { @class = "form-control"})

}  
ste-fu
  • 6,879
  • 3
  • 27
  • 46