1

I have been looking for some answers in this site to my question and I have found some related ones but any solve my troubble. I will appreciate any help. I send a list from my controller to the view and I get it fill but when I send it back to the controller I get it empty (Count=0).

Here is my Model...

 public class PriceListIndex
{
    public List<PriceList> PriceLists { get; set; } 
    public List<NewPriceList> NewPriceLists { get; set; }
    public int BlindTypeId { get; set; }
    public string BlindType { get; set; }
    public int GroupId { get; set; }
    public string Group { get; set; }
}

Here is my view...

@model A_Palace_Interior.ViewModel.PriceListIndex

@{
  ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm("Create", "PriceLists",FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
@Html.HiddenFor(model => model.BlindTypeId)
@Html.HiddenFor(model => model.GroupId)
@Html.HiddenFor(model => model.PriceLists)
@Html.HiddenFor(model => model.NewPriceLists)
<h4>Price List</h4>
<h5>Blind Type: @Model.BlindType</h5>
<h5>Group: @Model.Group</h5>

<table class="table-bordered">
    @{
        List<double> widths = ViewBag.Width;
        List<double> heights = ViewBag.Height;
    }
    <tr>
        <th rowspan="2" style="align-content: center">Length</th>
        <th colspan=@widths.Count style="align-content: center">Width</th>
    </tr>
    <tr>
        @{
            foreach (var width in widths)
            {
                <td>@width</td>
            }
        }
    </tr>
    @{
        foreach (var height in heights)
        {
            <tr>
                <td>@height</td>

                @foreach (var width in widths)
                {                   
                    if (Model.PriceLists.Exists(pl => pl.BlindDimensions.Width == width && pl.BlindDimensions.Height == height))
                    {
                        var priceList = Model.PriceLists.Find(pl => pl.BlindDimensions.Width == width && pl.BlindDimensions.Height == height);
                        <td>
                            @Html.EditorFor(model=>priceList.Price)
                            @Html.ValidationMessageFor(model => priceList.Price, "", new {@class = "text-danger"})
                        </td>
                    }
                    else
                    {
                        var newPriceList = new NewPriceList();
                        newPriceList.Height = height;
                        newPriceList.Width = width;
                        Model.NewPriceLists.Add(newPriceList);
                        <td>
                            @Html.EditorFor(model => model.NewPriceLists.Last().Price)
                            @Html.ValidationMessageFor(model => model.NewPriceLists.Last().Price, "", new {@class = "text-danger"})
                        </td>
                    }
                }
            </tr>
        }

    }       
</table>

<input type="submit" value="Save" class="btn btn-default"/>

Here is my controller...

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create(PriceListIndex priceListIndex)
    {
        if (ModelState.IsValid)
        {
            //code here
        }

    }

Thanks in advance...

Nely
  • 11
  • 1
  • 1
    `NewPriceLists` is a collection of complex objects and you cannot bind that to a hidden input (inspect the html your generating to understand why it will not bind). You need a `for` loop or `EditorTempate` for type of `NewPriceList` - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Dec 01 '16 at 21:00
  • Thanks a lot, it works!! – Nely Dec 08 '16 at 16:17

0 Answers0