0
votes

I am attempting to pass a model with a list property from the view, to the post action. The list comes back null on post, but we need the list with the new items to update their information. My model looks like this:

    public class Person()
    {
    internal string Name {get;set;}
    internal decimal Age {get;set;}
    internal List<int> Rate {get;set;}
    }

The view is populated with their current information, and we use a foreach to iterate through the list, and produce text boxes for the editing capability. When the person hits submit after editing the desired values, the view will go to an [HttpPost] action, but the values are null.

The view is surrounded with a @using (Html.BeginForm(Model)) and on submit, the model is passed to the post action, but the list is null. All other values are populated.

Any Ideas? P.S. we are using c# and Razor View Engine sorry but the list still is not passing to the model here is the 4 loop.

     @{ int i = 0;}
        <table>
        @for (i = 0; i < Model.VendorContracts.Count; i++ )
        {

            if (i == 0)
            {
                    <tr>
                        <th>
                            Vendor Contracts
                        </th>
                        <th>
                        Effective Date
                        </th>
                    </tr>

            }
            if (i < 5)
            {
                <tr>
                    <td>@Model.VendorContracts[i]
                    </td>

                    <td>@Html.TextBoxFor(model => model.EffectiveDates[i])</td>
                </tr>
            }
            if (i == 5 || i == 10 || i == 15 || i == 20)
            {
                @:</table>
                @:<table>
                }
            if (i >= 5)
            {
                if (i == 5 || i == 10 || i == 15 || i == 20)
                {
                <tr>
                <th>Vendor Contracts
                </th>
                <th>
                Effective Date
                </th>
                </tr>
                }
            <tr>
            <td>
            @Model.VendorContracts[i]
            </td>
            <td>@Html.TextBoxFor(model => model.EffectiveDates[i])</td>
            </tr>
            }
        } 
        </table>
2
Post your view as wellediblecode

2 Answers

1
votes

The problem is with your foreach loop. You should change it to a for loop and the bindings should work correctly.

Check these similar questions and answers:

ASP.NET MVC 4 - for loop posts model collection properties but foreach does not

MVC Razor @foreach

0
votes

I can give you a partial answer. To load the list items, so that they are returned on a HttpPost, you need to do this in the view

for(int idx = 0;idx < Model.Rate.Count;idx++)
{
    @Html.TextBox("Foo", Model.Rate[idx])
}

The key idea is @Html.TextBox("Foo", Model.Rate[idx]), you have to use an indexer when referencing the Rate list items. That way, when you post back, the MVC model binder will be able to pick up the list items, including any changes to those items.

As for picking up new items added by the user, I'm not sure about that. Maybe another SO user can help? But hopefully my answer helps somewhat.