1

I have dynamically loaded Partial Views (depends on "Add button"):

$.get('/Home/LoadSpread', {}, function (response) {
    var newHTML = response;
    $(newHTML).hide().appendTo("#Spreads").fadeIn(1000);
    });
$('#loadPartialViewBtn').hide().fadeIn(1000);

Each Partial View is rendered in main page form:

<form method="post" action="GetInitializedBranch">
    <div id="Spreads" align="center">
    </div>
    <input type="submit" value="Send" />
</form>

Each Partial View loads by same model (which should have different values on post):

public IActionResult LoadSpread()
{
    BranchView branchView = new BranchView
    {
       SelectorViewID = ++partialViewID,
       Spreads = new List<Spread>()
    };
    foreach (var rate in Handlers.DataAccessHandler.GetRatesFromDB())
    {
        branchView.Spreads.Add(new Spread { Curr = rate.Curr });
    }
    return PartialView("_Spread", branchView);
}

This is my Partial View model:

public class BranchView
{
    public int BranchID { get; set; }
    public string BranchName { get; set; }
    public int SelectorViewID { get; set; }
    public List<Spread> Spreads { get; set; }
}

This is the Partial view which is a table with unique input elements:

<table class="table table-bordered" style="margin-left: 480px; margin-top: 10px;">
        <tbody>
            <tr>
                <td valign="middle" rowspan="2"> Currency </td>
                <td style="text-align: center;" colspan="4">Cash</td>
                <td style="text-align: center;" colspan="4">Non Cash</td>
            </tr>
            <tr>
                <td>Spread Buy</td>
                <td>Buy Current</td>
                <td>Spread Sell</td>
                <td>Sell Current</td>
                <td>Spread Buy</td>
                <td>Buy Current</td>
                <td>Spread Sell</td>
                <td>Sell Current</td>
            </tr>

            @{ var j = 1;}

            @for (var i = 0; i < Model.Spreads.Count; i++)
        {
            var currencyName = Model.Spreads[i].Curr.ShortName;
            var cashSpreadBuy = "cashSpreadBuy" + currencyName + Model.Spreads[i].ID;
            var cashBuyCurrent = "cashBuyCurrent" + currencyName + Model.Spreads[i].ID;
            var cashSpreadSell = "cashSpreadSell" + currencyName + Model.Spreads[i].ID;
            var cashSellCurrent = "cashSellCurrent" + currencyName + Model.Spreads[i].ID;
            var nonCashSpreadBuy = "nonCashSpreadBuy" + currencyName + Model.Spreads[i].ID;
            var nonCashBuyCurrent = "nonCashBuyCurrent" + currencyName + Model.Spreads[i].ID;
            var nonCashSpreadSell = "nonCashSpreadSell" + currencyName + Model.Spreads[i].ID;
            var nonCashSellCurrent = "nonCashSellCurrent" + currencyName + Model.Spreads[i].ID;

            <tr>
                <td>
                    <div id="@currencyName">
                        @Html.DisplayTextFor(m => m.Spreads[i].Curr.ShortName)
                    </div>
                </td>
                <td>
                    @Html.EditorFor(m => m.Spreads[i].CashSpreadBuy,
                         new { htmlAttributes = new { @id = cashSpreadBuy, @type = "number", @step = "0.01", @class = "form-control" } })
                </td>
                <td>
                    <div id="@cashBuyCurrent">
                        @Html.DisplayTextFor(m => m.Spreads[i].CashBuyCurrent)
                    </div>
                </td>
                <td>
                    @Html.EditorFor(m => m.Spreads[i].CashSpreadSell,
                         new { htmlAttributes = new { @id = cashSpreadSell, @type = "number", @step = "0.01", @class = "form-control" } })
                </td>
                <td>
                    <div id="@cashSellCurrent">
                        @Html.DisplayTextFor(m => m.Spreads[i].CashSellCurrent)
                    </div>
                </td>
                <td>
                    @Html.EditorFor(m => m.Spreads[i].NonCashSpreadBuy,
                         new { htmlAttributes = new { @id = nonCashSpreadBuy, @type = "number", @step = "0.01", @class = "form-control" } })
                </td>
                <td>
                    <div id="@nonCashBuyCurrent">
                        @Html.DisplayTextFor(m => m.Spreads[i].NonCashBuyCurrent)
                    </div>
                </td>
                <td>
                    @Html.EditorFor(m => m.Spreads[i].NonCashSpreadSell,
                         new { htmlAttributes = new { @id = nonCashSpreadSell, @type = "number", @step = "0.01", @class = "form-control" } })
                </td>
                <td>
                    <div id="@nonCashSellCurrent">
                        @Html.DisplayTextFor(m => m.Spreads[i].NonCashSellCurrent)
                    </div>
                </td>
            </tr>
    }
        </tbody>
    </table>

I'm trying to get the array of BranchView models on post (i.e. BranchView[] branches), but I have no idea how to do that:

[HttpPost]
    public void GetInitializedBranch (BranchView[] branches)
    {
        //TODO: Collect All branches data 
    }

What should I do for getting all Partial Views user inputs results and get it as array of models?

David
  • 19
  • 7
  • You can create a partial view of partial views? Just include all your partials in the partial through listing `Html.RenderPartial("viewLocation", model)` – Wurd May 15 '18 at 09:22
  • I can't, each partial view must be loaded dynamically by ajax. – David May 15 '18 at 09:23
  • 1
    Why in the world are you generating all those `id` attributes (they are pointless and can be deleted). Are you saying that you are calling the `LoadSpread()` method multiple times? And you POST method accepts an array of `BranchView`, but you code is generating inputs for a single `BranchView` –  May 15 '18 at 09:28
  • Maybe look at this: https://stackoverflow.com/questions/16321736/how-can-i-post-a-list-of-items-in-mvc – Wurd May 15 '18 at 09:32
  • Usually the model argument uses `List`, e.g. `public ActionResult GetInitializedBranch (List branches)` and includes loop for each `BranchView` properties. Specifying `id` attribute inside `for` loop in view may generate invalid HTML. – Tetsuya Yamamoto May 15 '18 at 09:34
  • If you are wanting to generate and post back multiple `BranchView` items, then you name attributes need to be `name="[#].Spreads[#].CashSpreadBuy"` where `#` are collection indexers. Refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options, and [this one](http://stackoverflow.com/questions/40539321/partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for a complete example using `BeginCollectionItem` –  May 15 '18 at 09:47

0 Answers0