Search code examples
c#asp.net-corerazor-pagesienumerable

Cannot post IEnumerable data in .NET Core Web Application


I have a C# .NET Core web application (not an MVC web app). I’m trying to post data that is in an IEnumerable collection. For this post, I’ve simplified it:

In the CS file:

[BindProperty]
public IEnumerable<NpcpAllFields> NpcpAllFields { get; set; }

In the CSHTML file:

<form asp-page-handler="SaveData" method="post">
    <button type="submit" class="btn btn-primary" id="checkBtn" formmethod="post">Submit</button><br />
    <table class="table">
        <tr>
            <th>
                Sales Order Number
            </th>
            <th>
                Sales Order Line
            </th>
            <th>
                Is NPC
            </th>
        </tr>
        @foreach (var i in Model.NpcpAllFields)
        {
            <tr>
                <td>
                    <input class="form-control" name="@i" asp-for="@i.SALES_ORD_NO"  />
                </td>
                <td>
                    <input class="form-control" name="@i" asp-for="@i.SALES_ORD_LN_NO"  />
                </td>
                <td>
                    <input class="form-control" name="@i" asp-for="@i.IS_A_NEW_PART"  />
                </td>
            </tr>
        }
    </table>
</form>

The main code in question is the foreach:

@foreach (var i in Model.NpcpAllFields)
{
    <tr>
        <td>
            <input class="form-control" name="@i" asp-for="@i.SALES_ORD_NO"  />
        </td>
        <td>
            <input class="form-control" name="@i" asp-for="@i.SALES_ORD_LN_NO"  />
        </td>
        <td>
            <input class="form-control" name="@i" asp-for="@i.IS_A_NEW_PART"  />
        </td>
    </tr>
}

The above displays the data perfectly. But when I submit the data and check the NpcpAllFields collection in the debugger, it's empty. The problem might be the value in the name field (I tried many different ways):

name="@i" 

enter image description here

I was able to put the collection in a for loop (as opposed to a foreach) and it did populate the field in the debugger, but it does not display properly, it runs slow, and the data was incorrect.

I realize there are similar posts regarding this, but I could not find one that solved my issue. Many posts/answers were for .NET Core MVC apps.


Solution

  • You are providing the incorrect value to the name attribute. Thus the NpcpAllFields are not bonded with value when submitting the form. Inspect the <input> elements in your @foreach statement and you will see the elements are rendered as:

    <input class="form-control" name="<Namespace>.NpcpAllFields" />
    

    Place the @index on the top of the page (After @model).

    @{ int index = 0; }
    

    Render the name attribute as below:

    @foreach (var i in Model.NpcpAllFields)
    {
        <tr>
            <td>
                <input class="form-control" name="NpcpAllFields[@index].SALES_ORD_NO" asp-for="@i.SALES_ORD_NO"  />
            </td>
            <td>
                <input class="form-control" name="NpcpAllFields[@index].SALES_ORD_LN_NO" asp-for="@i.SALES_ORD_LN_NO"  />
            </td>
            <td>
                <input class="form-control" name="NpcpAllFields[@index].IS_A_NEW_PART" asp-for="@i.IS_A_NEW_PART"  />
            </td>
        </tr>
    
        index++;
    }