1

I have view page which has multiple partial views. Each partial view is a table with unique input values and each one takes same model with different id. And also I have one submit button for all those tables. How can I retrieve data? This is my partial view which can be rendered multiple time.

        @{ var i = 1;}

        @foreach (var spread in Model.Spreads)
        {
            var cashSpreadBuy = "cashSpreadBuy" + spread.Curr.ShortName + spread.ID;
            var cashBuyCurrent = "cashBuyCurrent" + spread.Curr.ShortName + spread.ID;
            var cashSpreadSell = "cashSpreadSell" + spread.Curr.ShortName + spread.ID;
            var cashSellCurrent = "cashSellCurrent" + spread.Curr.ShortName + spread.ID;
            var nonCashSpreadBuy = "nonCashSpreadBuy" + spread.Curr.ShortName + spread.ID;
            var nonCashBuyCurrent = "nonCashBuyCurrent" + spread.Curr.ShortName + spread.ID;
            var nonCashSpreadSell = "nonCashSpreadSell" + spread.Curr.ShortName + spread.ID;
            var nonCashSellCurrent = "nonCashSellCurrent" + spread.Curr.ShortName + spread.ID;

            spread.ID = i;
            i++;

            <tr>
                <td> @spread.Curr.ShortName </td>
                <td>
                    <input asp-for="@spread.CashSpreadBuy" class="form-control" id="@(cashSpreadBuy)" type="number" step="0.01"
                           onkeyup="Calculate('rateCashBuyCurrent', '@(cashSpreadBuy)', '@(cashBuyCurrent)', false, '@spread.Curr.ShortName')"
                           onmouseup="Calculate('rateCashBuyCurrent', '@(cashSpreadBuy)', '@(cashBuyCurrent)', false, '@spread.Curr.ShortName')" />
                </td>
                <td id=@(cashBuyCurrent)> @spread.CashBuyCurrent </td>
                <td>
                    <input asp-for="@spread.CashSpreadSell" class="form-control" id="@(cashSpreadSell)" type="number" step="0.01"
                           onmouseup="Calculate('rateCashSellCurrent', '@(cashSpreadSell)',  '@(cashSellCurrent)', false, '@spread.Curr.ShortName')"
                           onkeyup="Calculate('rateCashSellCurrent', '@(cashSpreadSell)',  '@(cashSellCurrent)', false, '@spread.Curr.ShortName')" />
                </td>
                <td id=@(cashSellCurrent)> @spread.CashSellCurrent </td>
                <td>
                    <input asp-for="@spread.NonCashSpreadBuy" class="form-control" id="@(nonCashSpreadBuy)" type="number" step="0.01"
                           onmouseup="Calculate('rateNonCashBuyCurrent', '@(nonCashSpreadBuy)',  '@(nonCashBuyCurrent)', false, '@spread.Curr.ShortName')"
                           onkeyup="Calculate('rateNonCashBuyCurrent', '@(nonCashSpreadBuy)',  '@(nonCashBuyCurrent)', false, '@spread.Curr.ShortName')" />
                </td>
                <td id=@(nonCashBuyCurrent)> @spread.NonCashBuyCurrent </td>
                <td>
                    <input asp-for="@spread.NonCashSpreadSell" class="form-control" id="@(nonCashSpreadSell)" type="number" step="0.01"
                           onmouseup="Calculate('rateNonCashSellCurrent', '@(nonCashSpreadSell)', '@(nonCashSellCurrent)', false, '@spread.Curr.ShortName')"
                           onkeyup="Calculate('rateNonCashSellCurrent', '@(nonCashSpreadSell)', '@(nonCashSellCurrent)', false, '@spread.Curr.ShortName')" />
                </td>
                <td id=@(nonCashSellCurrent)> @spread.NonCashSellCurrent </td>
            </tr>
        }

    </tbody>
</table>

And here is the block scheme of my view: enter image description here

David
  • 19
  • 7
  • Use the different IDs of the models to retrieve/update the correct entity. Can't help you any further without a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Georg Patscheider May 14 '18 at 07:47
  • I've updated the description. – David May 14 '18 at 07:58
  • You cannot use a `foreach` loop to generate form controls for a collection. You need a `for` loop or `EditorTemplate`. Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  May 14 '18 at 08:24

0 Answers0